Sanket and strings

2 test cases fail,whereas the output is correct.
here is my code

#include<bits/stdc++.h>
using namespace std;
int main() {
int i,maxcount=0;
long k;
cin>>k;
char str[100000];
cin>>str;
if(k>2){
while(k–){
for(i=0;i<strlen(str);i++){
if(str[i]==‘b’){
str[i]=‘a’;
break;
}
}
}

for(i=0;i<strlen(str);i++){
int count=1;
while(i<strlen(str)-1 && str[i]==str[i+1]){
count++;
i++;
}
if(count>maxcount){maxcount=count;}
}cout<<maxcount;
}
return 0;
}

here is my codehttps://ide.codingblocks.com/s/209522

you need to generalize your code a little bit.
problem in your approach. you just changed first k b’s into a and checked the longest equal substring. think about the case like ababababaaaaaaa and k=3, if you change first 3 b’s into a , you will only get 7 as answer. while if you have changed last 3 b’s into a, you would have got 13 as answer, which is expected one.

so the right approach is a bit more generalized version of yours.
step1. let us number the b’s from 1 to x… assuming x b’s are present.
step2. for i = 1 to x-k+1
2.1 change b’s numbered from i to i+k-1 to a.
2.2 check the longest substring with equal characters, say it y
2.3 ans = max(ans,y)
step3. return ans

Note: I am changing b’s to a’s. its because i am assuming that total no of b’s are less than a’s.
if no of b’s are larger, then convert the algorithm to change a’s into b’s.

thanks

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.