AAlgoLoopSpaced repetition for LeetCode
MEDIUMSliding WindowLeetCode ↗

Maximum Number of Vowels in a Substring of Given Length

The key idea

A fixed-length window of size k slides across the string one character at a time. Instead of recounting vowels for every window, keep a running count: when the window advances, add the new right character if it is a vowel and subtract the character that just left on the left. Track the maximum running count seen.

Problem

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'.

Constraints

Examples

Input: s = "abciiidef", k = 3 Output: 3
Input: s = "aeiou", k = 2 Output: 2
Input: s = "leetcode", k = 3 Output: 2

Complexity

Time: O(n) Space: O(1)

See the full solution

410310
Step-by-step visualization
Start free →

More Sliding Window problems