Skip to main content
Back to problems
Leetcode
Medium
Strings
Arrays
Maximum Number Of Vowels In A Substring Of Given Length

Find the maximum number of vowels in any substring of length kk.

Acceptance 100%
Problem Statement

Given a string and a fixed window length kk, examine every contiguous substring of length kk 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 k representing 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

  • 1≤k≤∣s∣1 \le k \le |s|
  • The string consists of lowercase English letters in the standard formulation
  • Time-efficient solutions should avoid scanning each substring independently
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.