I have my below solution failing only on test case 2.
void solve(){
SetIO("hps");
int n;
cin>>n;
vector<int>hoof_pre(n,0),hoof_suff(n,0);
vector<int>pap_pre(n,0),pap_suff(n,0);
vector<int>scis_pre(n,0),scis_suff(n,0);
for(int i=0;i<n;i++){
char c;
cin>>c;
if(c=='H') hoof_pre[i] = hoof_suff[i]=1;
else if(c=='P') pap_pre[i] = pap_suff[i]=1;
else scis_pre[i] = scis_suff[i]=1;
}
for(int i=1;i<n;i++){
hoof_pre[i]+=hoof_pre[i-1];
pap_pre[i]+=pap_pre[i-1];
scis_pre[i]+=scis_pre[i-1];
}
for(int i=n-2;i>-1;i--){
hoof_suff[i]+=hoof_suff[i+1];
pap_suff[i]+=pap_suff[i+1];
scis_suff[i]+=scis_suff[i+1];
}
int v =0;
for(int i=0;i<n-1;i++){
v = max(v,max(hoof_pre[i],max(hoof_pre[i]+pap_suff[i+1],hoof_pre[i]+scis_suff[i+1])));
v = max(v,max(scis_pre[i],max(scis_pre[i]+pap_suff[i+1],scis_pre[i]+hoof_suff[i+1])));
v = max(v,max(pap_pre[i],max(pap_pre[i]+hoof_suff[i+1],pap_pre[i]+scis_suff[i+1])));
}
int ans[] = {v,hoof_suff[n-1],pap_suff[n-1],scis_suff[n-1]};
for(int x : ans) v= max(v,x);
cout<<v;
}