Given a string str of length n. The problem is to find the length of the longest sub-string in str having frequency of each character less than equal to the given value k.
Input : str = "babcaag", k = 1 Output : 3 abc and bca are the two longest sub-strings having frequency of each character in them less than equal to '1'. Input : str = "geeksforgeeks", k = 2 Output : 10
Approach: Create an array freq of size 26 implemented as hash table to store the frequency of each character of str. Initialize all of its indexes with value ‘0’. Length of the string is n. Now implement the following algorithm.
longSubstring(str, k) Initialize start = 0 Initialize maxLen = 0 Declare ch for i = 0 to n-1 ch = str[i] freq[ch - 'a']++ if k < freq[ch - 'a'] then if maxLen < (i - start) then maxLen = i - start while (k < freq[ch - 'a']) freq[str[start] - 'a']-- start++ if maxLen < (n - start) then maxLen = n - start return maxLen
Length = 3
Time Complexity: O(n).
Auxiliary Space: O(1).
Because of the while loop the complexity might seem quadratic but if we look closely the inner while loop will traverse the string single time only.”
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 the largest substring which have character with frequency greater than or equal to half of the substring
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Longest equal substring with cost less than K
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Frequency of smallest character in first sentence less than that of second sentence
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Check if frequency of each character is equal to its position in English Alphabet
- Count of alphabets having ASCII value less than and greater than k
- Count the number of words having sum of ASCII values less than and greater than k
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- 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
- Longest Substring having equal count of Vowels and Consonants
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Substring of length K having maximum frequency in the given string
- Print number of words, vowels and frequency of each character
- Printing frequency of each character just after its consecutive occurrences
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.