Unable to solve

please provide sum hint

@kartik-malik
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’.

why are we starting j from k and i from 0

@kartik-malik because i is start of the window and j end (2nd point)

can u please explain through a example

also what will happen to characters beteen i and j

@kartik-malik its diff to explain(i mean dry run here) if you are having difficulty understanding it refer to the editorial code ( dry run it for some example) if any doubt in it ask


(its well commented every step)

does this question comes in easy category :sweat:

@kartik-malik yes i understand its a little tough to understand :sweat_smile:
but once done it will become clear. you will have to do it again after some time without looking for better grasp

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.