Sanket and Strings - help

Can you please explain this question? Its confusing to understand.

@amartya970914 Hi Amartya, according to the question,

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.

I hope the question is clear now.

2 Likes

Hey Amartya,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.

Sir, the given editorial code is in Java, its not clear to me. Can someone please give the running editorial in C++?

@amartya970914 hey amartya you open you challenge editorial?

@amartya970914 you can refer to this link for c++ implementation.

1 Like

Sir , can you please tell what’s the logic/approach behind the working of the above code. I mean , how did you find the solution?

@amartya970914 hey Amartya go through this link dry run of the c++ code.

2 Likes

what is the use of “IF” condition in line number 26??

do Input string have a and b equal

Can you dry run for an eg in which the if statement works? It would be really helpful. Thanks

@jaiskid I’ve understood your implementation, except for the logic behind:
if (min(freq[0], freq[1]) > k)

            {

                freq[str[left] - 'a']--;

                left++;

            }

Can you please explain what this code does?