Sanket and strings

getting testcase 0 and 1 wrong.

how to pass these type of strings?
2
aabbabbaaaaaaa
my output is 5

try to follow sliding window approach.

@Rakshitgarg99 actually your answer for this string is coming out to be 12, which is also incorrect answer

tell me how to solve the question correctly

@Rakshitgarg99 a few pointers
Always initialise your variables. You are doing operations like num1++ without even defining num1, that will give garbage values for sure in most compilers.
You are counting the total number of ‘a’ and ‘b’ in the string and replacing the first occurances of the least occuring characters. This approach will fail as more perfectness might be achieved if you replace the characters which occur later, or some other character for that matter.
Even with this approach, you are counting the length of the substring wrong. Do a dry run of your program and you will see your mistake. What you should do is, count the length of separate substrings with same characters and report the biggest length. Instead you are just incrementing the same variable without resetting its value or storing it anywhere for comparison purposes.

i know i will fail in test case like key 2 and aabbabbaaaaaaa. But i don’t get the way to solve can you please elaborate.

@Rakshitgarg99 You can solve this problem with help of two pointers. Let the first pointer be l and the second pointer be r. Then for every position l we will move to right end r until on the substring si,si + 1… sr it is possible to make no more than k swaps to make this substring perfect. Then we need to update the answer with length of this substring and move l to the right.

@Rakshitgarg99 you can refer this code for implementation https://ide.codingblocks.com/s/191713
Please mark your doubt as resolved if you are satisfied.

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.