What’s wrong with my code? is my logic wrong?
Sanket and strings
Hey @sounakume, Yes your logic is wrong you are assuming that all the characters are contiguous in nature.
For example in the case where k = 1 and string is abababab, answer should be 3 but your code would return 5.
Can you tell me what else approach can be used here? My approaches are not passing all cases.
Hey @sounakume 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.
But here u r considering only for a and b. What if there other alphabets??
It is mentioned in question that there are only 2 characters present that are a and b.
Ok its done, thank you.