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)
Auxilary 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)
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Longest substring of vowels
- Minimum length of substring whose rotation generates a palindromic substring
- Longest substring having K distinct vowels
- 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
- Lexicographically smallest and largest substring of size k
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- K-th lexicographically smallest unique substring of a given string
- Maximum length substring having all same characters after k changes
- Length of the longest valid substring
- Length of the longest substring that do not contain any palindrome
- Length of the longest substring with equal 1s and 0s
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.
Improved By : divyamohan123