Given string str containing only lowercase English alphabets and an integer K, the task is to find a K length substring which contains the maximum number of vowels (i.e. ‘a’, ‘e’, ‘i’, ‘o’, ‘u’). If there are multiple such substrings, return the substring which is lexicographically smallest.
Input: str = “geeksforgeeks”, K = 4
The substrings with maximum count of vowels are “geek”, “eeks” which contains 2 vowels. But “eeks” is lexicographically smallest.
Input: str = “ceebbaceeffo”, K = 3
Lexicographically substrings with maximum count of vowels are “ace”.
To solve the problem mentioned above we have to generate all the substrings of length K and store the lexicographically smallest of all such substrings which contains the maximum number of vowels.
Time Complexity: O(N2)
The above mentioned procedure can be optimized by creating a prefix sum array pref of vowels where the ith index contains the count of vowels from 0 to the ith index. The count of vowels for any substring str[l : r] can be given by pref[r]-pref[l-1]. Then, find the lexicographically smallest substring with the maximum count of vowels.
Below is the implementation of the above approach:
Time Complexity: O(N)
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Length of the smallest substring which contains all vowels
- Lexicographic smallest permutation of a String containing the second String as a Substring
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest and largest substring of size k
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Length of longest common subsequence containing vowels
- Longest Subsequence of a String containing only vowels
- Replace every vowels with lexicographically next vowel in a String
- Encrypt string with product of number of vowels and consonants in substring of size k
- Longest substring of vowels
- Longest substring having K distinct vowels
- Longest substring consisting of vowels using Binary Search
- Longest substring of vowels with no two adjacent alphabets same
- Longest Substring having equal count of Vowels and Consonants
- Shortest substring of a string containing all given words
- Smallest number containing all possible N length permutations using digits 0 to D
- Check if string can be made lexicographically smaller by reversing any substring
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
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.