Find the maximum number of vowels in any substring of length .
Given a string and a fixed window length , examine every contiguous substring of length and determine the largest number of vowels contained in any one of them. Vowels are typically considered to be a, e, i, o, and u.
The goal is to efficiently track the vowel count as the window moves across the string instead of recomputing the count from scratch for each substring.
Input Format
- A string
s - An integer
krepresenting the substring length
Assume the substring length is valid for the given string.
Output Format
- Return the maximum number of vowels found in any contiguous substring of length
k.
Constraints
- The string consists of lowercase English letters in the standard formulation
- Time-efficient solutions should avoid scanning each substring independently
Example 1
Input
s = "abciiidef", k = 3
Output
3
Explanation
The substring "iii" contains 3 vowels, which is the maximum possible.
Example 2
Input
s = "aeiou", k = 2
Output
2
Explanation
Any length-2 substring contains exactly 2 vowels, so the answer is 2.
Show 1 more example
Example 3
Input
s = "leetcode", k = 3
Output
2
Explanation
The best length-3 substrings such as "lee" and "eet" contain 2 vowels.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.