Some test cases are not passing
@aryanraj
Hello Aryan,
the approach u are trying is not correct.
for example
for test case like- abbbbbbbbbbaaaaaabbbbbbbb and k=2
your code will output-20
actual output will be - 12
hint-try 2 pointer approach
@aryanraj
also if u have any doubt in understanding problem refer this -
Basically, there is a string which consists of only a and b. A substring is a contiguous sequence of characters within a string. Our aim is to generate substrings of a and b by swapping characters such that the lengths of substrings of either a or b is maximum possible. The only constraint we have here is that only k swaps are allowed. So, you have to tell the maximum possible length of the substrings that can be generated. By swapping, we mean that a can be replaced by b and b can be replaced by a.
For example:
Consider the following string: abba and k = 2
So, we can make only two swaps
abba (swaps = 0)
aaba (swaps = 1)
aaaa (swaps = 2)
Thus, the maximum length of substring is 4.
Consider the string: ababab and k=2
ababab (swaps = 0)
aaabab (swaps =1)
aaaaab (swaps = 2)
Thus, the maximum length of substring is 5
tell me solution i am not getting
@aryanraj
hello aryan,
let’s say you have 2 pointers and both of them start from 0( 1-based indexing). Now maintain a count of numbers of ‘a’ and number of ‘b’ as the right pointer moves towards right. Keep moving the right pointer until the constraint min(count_a,count_b)<=K gets violated. Suppose the right pointer is at index i, so your possible answer can be i-1 as your left pointer is still at 0. Now move start moving your left pointer and change the count_A and count_B accordingly until the constraint gets satisfied.Once the constraint gets satisfied , repeat the whole process until the right pointer reaches N. This way you can keep count of all possible segment lengths and find out the maximum.
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.