Given a string str and an integer K, the task is to find the length of the longest sub-string S such that every character in S appears at least K times.
Input: str = “aabbba”, K = 3
In substring aabbba, each character repeats at least k times and its length is 6.
Input: str = “ababacb”, K = 3
There is no substring where each character repeats at least k times.
Naive Approach: We have discussed the Naive Approach in the previous post.
- Store the frequency of each characters of the given string in a frequency array of size 26.
- Initialize two variables start with 0 and end which is the length of the string str.
- Iterate over the string from start to end and count the number of times each character repeats and store it in an array.
- If any character repeats less than K times, then Divide the string into two halves. If i is the index of the string where we found that the string[i] repeats less than K times, then we divide the string into two halves from start to i and i + 1 to end.
- Recursively call for the two halves in the above steps i.e., from start to i and i + 1 to end separately and repeat the Step 2 and 3 and return the maximum of the two values reutnr by the above recursive call.
- If all the characters between start and end is repeated at least K times, then the answer is end – start.
- Largest sub-string where all the characters appear at least K times
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Remove characters that appear more than k times
- Given an array of size n and a number k, find all elements that appear more than n/k times
- Count words that appear exactly two times in an array of words
- Remove elements that appear strictly less than k times
- Remove elements from the array which appear more than k times
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Lexicographically largest subsequence such that every character occurs at least k times
- Longest substring with atmost K characters from the given set of characters
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Count all elements in the array which appears at least K times after their first occurrence
- Check if all occurrences of a character appear together
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Largest substring with same Characters
- Minimum number of operations to move all uppercase characters before all lower case characters
- Longest subsequence where every character appears at-least k times
- Longest subsequence where each character occurs at least k times
- Count of numbers appearing in the given ranges at-least K times
- Convert a number of length N such that it contains any one digit at least 'K' times
Below is the implementation of above approach:
Time Complexity: O(N*log2N)
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.
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.