I don't understand how to improve my code it is failing all test cases

Hi @10vaibhavsinghnegi,
Your approach is a bit faulty. See the test case:
2
aabbabaa
Here the correct output should be 5 but your code gives 4.

A better approach would be to start with l and r to mark left and right index of the string under inspection. Start from l=0, r=0, count=0, max = -1.

Start with r<length of string. Increment the count whenever you find a different character than the one you are using to build a string (example: if we are forming a string of an only, then b is a different char).

While count is greater than k, decrement the count by one if the element at i index is different.

Increment i.

Then, compare max with count for maximum value.

And finally, increment r.

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.