AlgoInsight

DSA problem

Maximum Number of Vowels in a Substring of Given Length

MediumSliding Window - Fixed SizePattern: Fixed-Size Sliding Windowtop600AmazonMicrosoftAdobe

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, and u.

Since every candidate substring has the fixed length k, we can slide a window of that width across s and keep a running count of vowels inside it. When the window advances by one position, exactly one character enters and one leaves, so the vowel count updates in constant time. The answer is the largest count observed across all windows.

Examples

Example 1
Input: s = "abciiidef", k = 3
Output: 3
Explanation: The substring "iii" contains 3 vowels, which is the maximum possible for any window of length 3.
Example 2
Input: s = "aeiou", k = 2
Output: 2
Explanation: Any substring of length 2 consists entirely of vowels, so the maximum is 2.
Example 3
Input: s = "leetcode", k = 3
Output: 2
Explanation: The substring "lee" contains 2 vowels, as do "eet" and "ode"; no window of length 3 has more.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

sliding-windowstring

Browse all DSA problems