Not able to solve SanketAndStrings problem

Sir I am not getting any approach to solve the SanketAndStrings problem.Please tell me some approach to solve the problem.

Hi Rishabh
initialize two variables l(left index) and r(right index)
in this we traverse the whole string and if we find a different character we increase the count and when count becomes greater than k at the right index we again start from 0th index and if we find a different character we decrease the count.
when count becomes equal to k(at left index) then length =r+l-1
repeat this for the entire length

@rishu7865 hey rishabh
Sanket has a string consisting of only a and b as the characters. Sanket describes perfectness of a string as the maximum length substring of equal characters. Sanket is given a number K which denotes the maximum number of characters he can change. Find the maximum perfectness he can generate by swapping no more than k characters.

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.

1 Like