Can you help me in developing an approach to this problem?
Sanket and strings
This seems to be a tough problem at first, but driving deep we see that just 2 char โaโ , โbโ have to be manipulated , so function call can be made
what has to be done now?
max(udpate_string(str, โaโ) +update_string(str, โbโ))
think in terms of SLIDING WINDOW ( we maintain track of l , r pointers and k , count = 0 )
EXPAND the window , do contraction , continue to compute max window size of perfect string at each instance
update_string(str , char ch, int k ){
long l = 0 , r = 0 , n = s.size();
long count = 0;
long max_ans =0;
while( r< n ){
if(s[r] != ch)
count+=1;
while(count > k){
if(s[l] != ch)
countโ;
l+=1;
}
max_ans = max(max_ans , r-l+1);
r+=1;
}
return max_ans;
}
continue iterating until a non matching char is found, at each step compute max value of perfect string
if(non matching char > k )
contract the window size from left until count of nonmatching character <= k
again iterate further.
Please ping me in case u have further doubts.