We have a string of length n, which consist only UPPER and LOWER CASE characters and we have a number k (always less than n and greater than 0). We can make at most k changes in our string such that we can get a sub-string of maximum length which have all same characters.
n = length of string k = changes you can make Input : n = 5 k = 2 str = ABABA Output : maximum length = 5 which will be (AAAAA) Input : n = 6 k = 4 str = HHHHHH Output : maximum length=6 which will be(HHHHHH)
We check for each character of English alphabet (both upper and lower cases one by one). We are basically looking for maximum length of sub-string that can be formed by each character and whichever character will form the sub-string of maximum length then that length will be our answer.
- We check for maximum length of sub-string that can be formed by every character in a set of 52 characters (From ‘A’ to ‘Z’ and from ‘a’ to ‘z’).
- For doing this we traverse whole string and whenever we find a different character, we increase the count.
- 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.
- When count will be equal to k (at left index) then at that point the length will be rightIndex-leftIndex+1.
- We repeat this process until we reach at the end of string and at that point we will return the maximum length.
- We do this for all characters and finally return the maximum length.
Maximum length = 5 Maximum length = 6
This article is contributed by Niteesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum length substring with exactly K distinct characters
- Length of the longest substring without repeating characters
- Find length of longest substring with at most K normal characters
- Maximum length substring with highest frequency in a string
- Maximum length palindromic substring such that it starts and ends with given char
- Maximum length palindrome that can be created with characters in range L and R
- Maximum count of sub-strings of length K consisting of same characters
- Maximum length of balanced string after swapping and removal of characters
- Length of the smallest sub-string consisting of maximum distinct characters
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Longest substring with atmost K characters from the given set of characters
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K