Perfectess of a string

Given a string consisting of characters from ‘a’ to ‘z’ as the characters. Perfectness of a string is the maximum length substring of equal characters. Given a number k which denotes the maximum number of characters that can be changed. Find the maximum perfectness that can be generated by changing no more than k characters.

Input Format
The first line contains an integer denoting the value of K. The next line contains a string having only ‘a’ to ‘z’ as the characters.

Constraints
2 ≤ N ≤ 10^6

Output Format
A single integer denoting the maximum perfectness achievable.

Sample Input
2
aacyyyyb
Sample Output
6

2
abba
Sample Output
4

Explanation
We can swap the a’s to b using the 2 swaps and obtain the string “bbbb”. This would have all the b’s and hence the answer 4.
Alternatively, we can also swap the b’s to make “aaaa”. The final answer remains the same for both cases.

Explain the algorithm.

You have to use Sliding window approach to solve this

Detailed Algorithm

  1. We check for maximum length of sub-string that can be formed by every character in a set of 26characters ( from ‘a’ to ‘z’).
  2. For doing this we traverse whole string and whenever we find a different character, we increase the count.
  3. If count becomes greater than k (at right index), we again start from 0th index and if we found different character we will decrease the count.
  4. When count will be equal to k (at left index) then at that point the length will be rightIndex-leftIndex+1.
  5. We repeat this process until we reach at the end of string and at that point we will return the maximum length.
  6. We do this for all characters and finally return the maximum length.

Code

i hope this help
if you have any doubt feel free to ask doubt

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.