Sanket and strings

#include
#include
#include
using namespace std;

int main() {
int n;
cin>>n;
cin.get();
char a[10000];
cin>>a;
int m;

m=strlen(a);
sort(a,a+m);
//cout<<a;
int s=0,d=0;
for(int i=0;i<m;i++){
if((int)a[i]==97){
s++;
}
else if((int)a[i]==98){
d++;
}
//cout<<s<<" "<<d<<endl;

}
int f=0,temps=0,tempd=0;
f=max(s,d);
temps=s+min(d,n);
tempd=d+min(s,n);
f=max(temps,tempd);
cout<<f;
return 0;
}

2 test cases fail
cant understand why

@goelsparsh277 hey sorting is not allowed, on sorting you will change the string. Also if one allows sorting then it can be simply by counting a and b.
You should look at the frequency of both a and b and keep a variable at start, and which ever frequency is less than k, you make the string of that char (no need to actually transform the string)
Now when min frequency is greater than k, move your start position and decrement the frequencies accordingly till one of them becomes less than k. Refer to this code


If this resolves your doubt mark it as resolved

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.