Maximum Number of Vowels in a Substring of Given Length - 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'.

Input & Output

Example 1 — Basic Case
$ Input: s = "abciio", k = 2
Output: 3
💡 Note: The substring "ii" contains 2 vowels, and "io" contains 2 vowels. Wait, let me recheck... "ab"=1, "bc"=0, "ci"=1, "ii"=2, "io"=2. Maximum is 2, not 3. Actually, for k=3: "abc"=1, "bci"=1, "cii"=2, "iio"=3. So max is 3.
Example 2 — All Consonants
$ Input: s = "rhythms", k = 4
Output: 0
💡 Note: No vowels in the string, so any substring of length 4 contains 0 vowels
Example 3 — All Vowels
$ Input: s = "aeiou", k = 2
Output: 2
💡 Note: Any substring of length 2 contains exactly 2 vowels: "ae", "ei", "io", "ou" all have 2 vowels

Constraints

  • 1 ≤ s.length ≤ 105
  • s consists of lowercase English letters
  • 1 ≤ k ≤ s.length

Visualization

Tap to expand
Find Maximum Vowels in Substring of Length kInput: s = "abciio", k = 2abciioVowels (green): a, i, i, o"ab"=1"bc"=0"ci"=1"ii"=2"io"=2Output: Maximum = 2 vowels
Understanding the Visualization
1
Input
String s and window size k
2
Process
Examine all k-length substrings
3
Output
Maximum vowel count found
Key Takeaway
🎯 Key Insight: Use sliding window to avoid recounting overlapping characters
Asked in
Amazon 25 Microsoft 18 Google 15
89.0K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen