Sanket and stringprob

#include
using namespace std;

int main() {
int k,n=0;
cin>>k;
string s;
cin>>s;
n=s.length();
if(n-k==2)
cout<<n;
else
cout<<k+2;
}
this code is right but is is not passing test cases

Hi this question will be solved from different approach.
You can use a variable left which initially points towards 0.Now make an array say count of size 2 in which you keep a count of the frequency of both ‘a’ and ‘b’ and simultaneously keep on checking that if min number from count array is greater than k
then decrement the value at left-‘a’ index of count and increment left(This is done because we cannot take more than k steps to make the string beautiful) else you just increment ans and in the end print ans.

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.