Sanket and string ; your solution is being judged

it is running fine in sublime , whats the prob in the code ??
code link-https://ide.codingblocks.com/s/106420

@Ajaypandey
Try these testcases :
Input :
1
aaaa

Expected Output : 4

Input :
1
baabaaab

Expected Output : 6

Your code doesn’t give any output for either of them.

i did that previously and only test case got passed ( i removed n<2 ) condition and after removing it code works fine and whats the N in constraints ??

I do not understand the condition you are talking about. Please share your code.
As for the constraints.
n<=l
Where l is the length of the string. It is ensured that both n and l will fit I range of int and are positive integers.

code link-https://ide.codingblocks.com/s/106505
this working fine in sublime for all cases(including the two you mentioned above)

@Ajaypandey
Consider this testcase :
Input :
2
aaabbbaaaa

Expected Output :
6

Your Output :
9

Your code is considering all a’s together and all b’s together while we have to consider each window of characters. We should not take a count of all a’s or all b’s together as that would give us wrong answer.
I suggest you to use two pointer approach for this problem.
Alternatively you can use Deque ( if you have done Stacks & Queues in your course ).

i will use two pointer method , but can you pls tell what iam doing wrong here ?
code link - https://ide.codingblocks.com/s/107903

@Ajaypandey
As I stated in my previous reply , you are considering the count of all A’s and B’s together rather than their count in each window. This ruins your answer as the A’s or B’s might be seperated and distance apart that it is not feasible to consider them together.
In this testcase
2
aaabbbaaaa

Your code sees that count of A is greater than count of B and then uses this total countOfA to produce the result. However that is the problem since in this count , it has counted the A’s on the left and the right together. However the A’s on left and right can never occur in a single window if only 2 swaps are allowed. Hence your code gives the wrong answer.

i got your point , now how should i approach this ? i did see some codes but couldnt understand , pls help .

@Ajaypandey
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’.