Given a string s we have to find the length of the longest substring of s which contain exactly K distinct vowels.
Note: Consider uppercase and lowercase characters as two different characters.
Input : s = “tHeracEBetwEEntheTwo”, k = 1
Output : 14
Explanation : Longest substring with only 1 vowel is “cEBetwEEntheTw”
and its length is 14.
Input : s = “artyebui”, k = 2
Output : 6
Explanation : Longest substring with only 2 vowel is “rtyebu”
Brute-Force Approach: For each substring, we check for the criteria for K distinct vowel and check the length. Finally, the largest length will be the result.
Efficient Approach : Here we maintain the count of vowels occurring in the substring. Till K is not zero, we count the distinct vowel occurring in the substring. As K becomes negative, we start deleting the first vowel of the substring we have found till that time, so that it may be possible that new substring( larger length ) is possible afterward. As we delete the vowel we decrease its count so that new substring may contain that vowel occurring in the later part of the string. And as K is 0 we get the length of the substring.
Below is the implementation of the above approach
- Longest Substring having equal count of Vowels and Consonants
- Longest substring of vowels
- Longest substring consisting of vowels using Binary Search
- Longest substring of vowels with no two adjacent alphabets same
- Find the String having each substring with exactly K distinct characters
- Length of longest substring having all characters as K
- Encrypt string with product of number of vowels and consonants in substring of size k
- Lexicographically smallest K-length substring containing maximum number of vowels
- Length of the smallest substring which contains all vowels
- Length of longest common subsequence containing vowels
- Longest Ordered Subsequence of Vowels
- Longest Subsequence of a String containing only vowels
- Longest subsequence consisting of alternate vowels and consonants
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- 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
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Minimum length substring with exactly K distinct characters
- 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 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.