Given string str consisting of only lowercase English alphabets, the task is to find the substring of the 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.
The below code is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
- Maintain an array count to store the 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 the start is not a vowel or it is a vowel with a 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
The below code implements the above approach:
Time Complexity: O(N)
- Length of smallest substring of a given string which contains another string as subsequence
- Lexicographically smallest K-length substring containing maximum number of vowels
- Program to accept a Strings which contains all the Vowels
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Modify the string such that it contains all vowels at least once
- Minimum K such that every substring of length atleast K contains a character c
- Minimum length of substring whose rotation generates a palindromic substring
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Longest substring of vowels
- Encrypt string with product of number of vowels and consonants in substring of size k
- 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
- Minimum flips required in a binary string such that all K-size substring contains 1
- Smallest non-zero substring which has any permutation divisible by 2^K
- Check if a String contains Anagrams of length K which does not contain the character X
- Find length of longest subsequence of one string which is substring of another string
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
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.