Please tell me where im getting wrong all the test cases according me are correct and with deep respect please tell me variation in this code not a new concept please.
Please tell me where im getting wrong all the test cases according me are correct and please tell me variation in this code not a new concept please
mam by my code im getting 3 not 4
my bad,for test case
2
abbbababbaba
answer should be 8 not 9
similarly for
2
aabbaababba
ans should be 6
debug for this and let me know if this doesn’t help
for test case 2 abbbababbaba
the maximum of a or b is 7 because b is occurring 7 times and since k is equal to 2 then we can replace to ‘a’ to get 9 'b’finally how the answer can be 8?? please explain
look at the input carefully
- abbbababbaba is the input
- abbbbbbbbaba is the reason for 8
yes there are 7 b and u can replace 2 a but the only way you can replace them to get a maximum length substring(continuous) of equal characters gives us 8
The code is complicated but the outputs should be correct please check it
dry run your code for this test case:
1
abbaa
correct ouput should be 2
your code is not ideally correct:
as you have tried a lot you can read the correct way to solve the question and move forward and remember it
Approach - Two pointer approach
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’.