SanketAndStrings wrong answer

2 testcases wrong out of 3.

Hey @vivek21,

This approach will give you WA for sure, as you may miss certain test cases.
A better approach would be:

Take 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.

For a string “aabbabaa” with k=2, what should be correct answer, 5 or 7?

Hi @vivek21,
The answer should be 5 in that case.

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.