Given a string str consisting of only lowercase English alphabets, the task is to find the substring of smallest length which contains all the vowels. If no such substring is found, print -1.
Input: str = “babeivoucu”
Explanation: Smallest substring which contains each vowel atleast once is “abeivou” of length 7.
Input: str = “abcdef”
Explanation: No such substring is found.
- Store the frequencies of each vowel and the indices at which the vowels are present.
- If all the vowels are not present, straightaway print -1.
- Take two pointers i and j at the first and last indices containing a vowel.
- If the frequencies of vowel at ith and jth index exceed 1, decrease the count of that vowel and shift i and j to the right and left respectively to the next index containing a vowel.
- If the frequencies of vowel at ith or jth index is equal to 1, set flag1 or flag2 respectively and continue.
- Once both flag1 and flag2 are set, the length of the substring cannot be further minimized. The distance between the current indices pointed by the two pointers denotes the result.
Below code is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
- Maintain an array count to store frequency of each vowel.
- Maintain a start variable to store the starting index of the current substring.
- Iterate over the string and do the following:
- Increase the frequency of the character if it is a vowel.
- Move start as right as possible based on the condition that either the character at start is not a vowel or it is a vowel with frequency greater than 1 which means that it has appeared previously.
- Once the start is moved as far as possible, check if all vowels are present in the substring between start and the current index.
- Whenever the above condition satisfies, update the minimum length of such substring obtained
- Print the minimum length of substring obtained or print -1 if no such substring is obtained
Below code implements the above approach:
Time Complexity: O(N)
- Lexicographically smallest K-length substring containing maximum number of vowels
- 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
- 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
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Encrypt string with product of number of vowels and consonants in substring of size k
- Length of longest common subsequence containing vowels
- Smallest non-zero substring which has any permutation divisible by 2^K
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Lexicographically smallest and largest substring of size k
- Length of the longest valid substring
- Length of the longest substring that do not contain any palindrome
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest substring with equal 1s and 0s
- Maximum length substring having all same characters after k changes
- Find max length odd parity substring
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.