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 of vowels
- Length of the smallest substring which contains all vowels
- Encrypt string with product of number of vowels and consonants in substring of size k
- Longest Ordered Subsequence of Vowels
- Longest Subsequence of a String containing only vowels
- Length of longest common subsequence containing vowels
- Find the String having each substring with exactly K distinct characters
- Minimum length substring with exactly K distinct characters
- Count of distinct characters in a substring by given range for Q queries
- Longest Palindromic Substring | Set 2
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 1
- Longest substring with count of 1s more than 0s
- Longest Common Substring | DP-29
- Longest repeating and non-overlapping 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.