Maximum Number of Vowels in a Substring of Given Length

MediumSliding Windowsliding window

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a lowercase string s and an integer k with 1 <= k <= s.length, consider every substring of s of length exactly k. Return the largest number of vowels ('a', 'e', 'i', 'o', 'u') found in any one of those substrings. Aim for O(n) time.

Examples

Input: s = "abciiidef", k = 3
Output: 3
The window iii contains three vowels.
Input: s = "aeiou", k = 2
Output: 2
Every window of length 2 is made of two vowels.
Input: s = "rhythms", k = 4
Output: 0
The string has no vowels.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters
  • 1 <= k <= s.length

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More sliding window problems