Given a 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
- Length of the smallest substring which contains all vowels
- Lexicographically smallest and largest substring of size k
- K-th lexicographically smallest unique substring of a given string
- Encrypt string with product of number of vowels and consonants in substring of size k
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Replace every vowels with lexicographically next vowel in a String
- Longest substring of vowels
- Longest substring having K distinct vowels
- Longest substring of vowels with no two adjacent alphabets same
- Longest substring consisting of vowels using Binary Search
- Check if string can be made lexicographically smaller by reversing any substring
- Maximum splits in binary string such that each substring is divisible by given odd number
- Maximum number of set bits count in a K-size substring of a Binary String
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Lexicographically smallest string after M operations
- Construct lexicographically smallest palindrome
- Lexicographically Smallest Topological Ordering
- Lexicographically smallest string of length N and sum K
- Lexicographically smallest rotated sequence | Set 2
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.