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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Length of longest substring having all characters as K
- Substring of length K having maximum frequency in the given string
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Minimum changes required to make first string substring of second string
- Maximum non-repeating characters after removing K 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
- Min flips of continuous characters to make all characters same in a string
- Minimum characters to be replaced to make frequency of all characters same
- Maximum count of sub-strings of length K consisting of same characters
- Minimum steps to delete a string by deleting substring comprising of same characters
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Longest substring such that no three consecutive characters are same
- Largest substring with same Characters
- Maximum length of balanced string after swapping and removal of characters
- Find the String having each substring with exactly K distinct characters
- Longest substring with atmost K characters from the given set of characters
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest substring with no consecutive same letters
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring