Maximum Number of vowels in a substring of given Length.
Maximum Number of Vowels in a Substring of Given Length
Given a string
s and an integer
k.Return the maximum number of vowel letters in any substring of
s with length
k.Vowel letters in English are (a, e, i, o, u).
Input: s = "abciiidef", k = 3
Explanation: The substring "iii" contains 3 vowel letters.
Input: s = "aeiou", k = 2
Explanation: Any substring of length 2 contains 2 vowels.
Input: s = "leetcode", k = 3
Explanation: "lee", "eet" and "ode" contain 2 vowels.
Input: s = "rhythms", k = 4
Explanation: We can see that s doesn't have any vowel letters.
Input: s = "tryhard", k = 4
We can solve this problem by using sliding window algorithm. We have list of values and given size of window.we need to slide the window one step at a time. First we need to calculate the number of vowels in first window for starting to Kth element. Afterward we will slide window one value at a time.
while sliding we need to remove first value from window and if that value is vowel , we need to decrement the current running vowel by one. Similarly we need to add a new value at the end and if that value is vowel we need to increment the current running vowel.
While doing that , we need to keep track of max number of vowels.