What else I have to add in it
Hey @ashish_arora369
You approach is not correct.
say a>b
then
assume string is like this bbbbbbbaaaaaa for some k=2
now check if your code will correct answer or not.
Follow this approach:
You can solve this problem in O(n) time using the two pointer approach.
- Make two variabes , say i and j .
- i defines the beginning of a window and j defines its end.
- Start i from 0 and j from k.
- Letβs talk about the singular case when we are considering the max window for only 'aβs and consider only the swapping of b-> a. If we are able to get the answer for max window of consecutive 'aβs , we can simply implement the same algo for the max βbβ window as well.
- So we started i from 0 and j from k.
- Move j ahead freely as long as there are βaβ characters at s[ j ] position.
- Maintain a count variable which counts the number of swaps made or the number of 'bβs in our A window.
- If you encounter a βbβ char at s[ j ] position , increment the count variable. Count should never exceed k .
- Take the size of the window at every point using length = j - i + 1;
- Compute the max size window this way and do the same for βbβ as well.
- Output the maximum size window of βaβ and βbβ.
bbbbbbbaaaaaa for some k=2 it gave answer 9 according to me which is right
No @ashish_arora369
Answer is 12 for that
We need to return max string length with equal chars
See last 12 char string
They have same no of both chars and we dont have to do any swap here
may you show me how you got that answer
Here is the correct algo , please refer to this 
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.