LeetCode #1456 Medium

Maximum Number of Vowels in a Substring

Maximum Number of Vowels in a Substring is LeetCode 1456 (Medium). Its full title adds "of Given Length". You get a string s of lowercase letters and an integer k. Return the largest number of vowels (a, e, i, o, u) found in any substring of exactly k letters.

  • A substring is a contiguous run of letters.
  • s can be 10⁵ letters long and k can be just as large, so counting every window letter by letter is too slow.
Constraints
  • 1 <= s.length <= 10⁵
  • s consists of lowercase English letters.
  • 1 <= k <= s.length
stringsliding-window
Open on LeetCode ↗
02

Intuition

Write 1 under every vowel and 0 under every other letter. The vowel count of a window is the sum of the numbers under it, so the task is the largest sum of k neighbouring 0s and 1s.

  • One letter in, one letter out: moving the window one place right adds the new letter on the right and drops the old letter on the left. The count changes by at most 1, and only those two letters decide how.
  • A ceiling of k: a window of k letters cannot hold more than k vowels. Once some window is all vowels, nothing later can beat it, so the scan can stop there.
How to spot this pattern

A fixed substring length and a count of letters with some property inside it points to a fixed-size sliding window that updates one letter in and one letter out.

03

Approach

Try it first

Before reading on, decide how the vowel count of the window starting at i + 1 follows from the count of the window starting at i, using only two letters. Then find the largest value the count can ever reach. Aim for O(n) time and O(1) space.

1

Count vowels in the first window

Count the vowels in s[0] to s[k − 1] and let best start at that count. This is the only window counted letter by letter; every later count is derived from the one before it.

2

Add the letter that enters

For i from k to n − 1, the letter s[i] joins the window on the right. Add 1 to count if it is a vowel, because the window now contains it.

3

Drop the letter that leaves

Subtract 1 if s[i − k] is a vowel. With s[i] added, the window covers i − k + 1 to i, so s[i − k] is the letter that just fell off the left end.

4

Keep the best and stop at k

Set best to the larger of best and count. If best equals k, return k at once: a window of k letters has at most k vowels, so no later window can do better.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def maxVowels(self, s: str, k: int) -> int:
▶3 vowels = set("aeiou")
▶4 count = sum(ch in vowels for ch in s[:k])
▶5 best = count
▶6 for i in range(k, len(s)):
▶7 count += s[i] in vowels
▶8 count -= s[i - k] in vowels
▶9 best = max(best, count)
▶10 if best == k:
▶11 return k
▶12 return best
l0e1e22t3c4o5d6e7vowels per windowvowelcount2bestmost possible3first window 'lee' has 2 vowels
k3
count2letters 0 to 2
Count the vowels in the first 3 letters, lee: there are 2. Its bar sits under the window's last letter. This is the only window counted letter by letter.
l0e1e22t3c4o5d6e7vowels per windowvowelcount2best2most possible3best starts at 2
best2
The first window is a real window, so the best starts at its count, 2. A window of 3 letters can hold at most 3 vowels, the ceiling shown on the right.
l0e1e22t3inc4o5d6e7vowels per windowvowelcount2best2most possible3't' enters, not a vowel → count stays 2
i3
s[i]tnot a vowel
count2
The letter at index 3, t, joins the window on the right. It is not a vowel, so the count stays 2. For a moment the window holds 4 letters; the next line removes one.
l0oute1e22t32c4o5d6e7vowels per windowvowelcount2best2most possible3'l' leaves, not a vowel → count stays 2
s[i - k]lnot a vowel
count2
The letter at index 0, l, falls off the left end. It was not a vowel, so the count stays 2. The window is now eet, and its bar goes under index 3.
l0e1e22t32c4o5d6e7vowels per windowvowelcount2best2most possible32 is not above 2 → best stays 2
best2
This window has 2 vowels, not more than the best of 2, so the best stays.
l0e1e22t32c4ino5d6e7vowels per windowvowelcount2best2most possible3'c' enters, not a vowel → count stays 2
i4
s[i]cnot a vowel
count2
The letter at index 4, c, joins the window on the right. It is not a vowel, so the count stays 2. For a moment the window holds 4 letters; the next line removes one.
l0e1oute22t32c41o5d6e7vowels per windowvowelcount1best2most possible3'e' leaves, a vowel → count 1
s[i - k]evowel
count1
The letter at index 1, e, falls off the left end. It was a vowel, so the count drops to 1. The window is now etc, and its bar goes under index 4.
l0e1e22t32c41o5d6e7vowels per windowvowelcount1best2most possible31 is not above 2 → best stays 2
best2
This window has 1 vowel, not more than the best of 2, so the best stays.
l0e1e22t32c41o5ind6e7vowels per windowvowelcount2best2most possible3'o' enters, a vowel → count 2
i5
s[i]ovowel
count2
The letter at index 5, o, joins the window on the right. It is a vowel, so the count goes up to 2. For a moment the window holds 4 letters; the next line removes one.
l0e1e2out2t32c41o51d6e7vowels per windowvowelcount1best2most possible3'e' leaves, a vowel → count 1
s[i - k]evowel
count1
The letter at index 2, e, falls off the left end. It was a vowel, so the count drops to 1. The window is now tco, and its bar goes under index 5.
l0e1e22t32c41o51d6e7vowels per windowvowelcount1best2most possible31 is not above 2 → best stays 2
best2
This window has 1 vowel, not more than the best of 2, so the best stays.
l0e1e22t32c41o51d6ine7vowels per windowvowelcount1best2most possible3'd' enters, not a vowel → count stays 1
i6
s[i]dnot a vowel
count1
The letter at index 6, d, joins the window on the right. It is not a vowel, so the count stays 1. For a moment the window holds 4 letters; the next line removes one.
l0e1e22t3out2c41o51d61e7vowels per windowvowelcount1best2most possible3't' leaves, not a vowel → count stays 1
s[i - k]tnot a vowel
count1
The letter at index 3, t, falls off the left end. It was not a vowel, so the count stays 1. The window is now cod, and its bar goes under index 6.
l0e1e22t32c41o51d61e7vowels per windowvowelcount1best2most possible31 is not above 2 → best stays 2
best2
This window has 1 vowel, not more than the best of 2, so the best stays.
l0e1e22t32c41o51d61e7invowels per windowvowelcount2best2most possible3'e' enters, a vowel → count 2
i7
s[i]evowel
count2
The letter at index 7, e, joins the window on the right. It is a vowel, so the count goes up to 2. For a moment the window holds 4 letters; the next line removes one.
l0e1e22t32c4out1o51d61e72vowels per windowvowelcount2best2most possible3'c' leaves, not a vowel → count stays 2
s[i - k]cnot a vowel
count2
The letter at index 4, c, falls off the left end. It was not a vowel, so the count stays 2. The window is now ode, and its bar goes under index 7.
l0e1e22t32c41o51d61e72vowels per windowvowelcount2best2most possible32 is not above 2 → best stays 2
best2
This window has 2 vowels, not more than the best of 2, so the best stays.
l0e1e22t32c41o51d61e72vowels per windowvowelcount2best2most possible3tallest bar is 2 → answer 2
return2
Every window has been checked. The tallest bar is 2, first reached by lee, so the answer is 2.
05

Common pitfalls

Dropping the wrong letter

✗ Wrong
count -= s[i - k + 1] in vowels
✓ Right
count -= s[i - k] in vowels

s[i − k + 1] is still inside the window, so it is subtracted while it is counted. For s = "ba", k = 1 the window "a" adds 1 and then subtracts its own a, and the code returns 0 instead of 1.

Sliding from index 0 in Python

✗ Wrong
count = 0
for i in range(len(s)):
    count += s[i] in vowels
    count -= s[i - k] in vowels
✓ Right
count = sum(ch in vowels for ch in s[:k])
for i in range(k, len(s)):

For i < k, s[i − k] is a negative index, and Python reads it from the end of the string instead of failing. For s = "ea", k = 1 the first step subtracts the final a, and the code returns 0 instead of 1.

06

Edge cases

k equals the length of s

The whole string is the only window. The loop from k to n − 1 is empty, so the answer is the vowel count of the first window.

No vowels anywhere

No letter ever adds 1, so count and best stay 0 and the answer is 0.

07

Complexity

Time
O(n)
Space
O(1)
The first window checks k letters, and each later move checks two letters. The vowel set has five entries, and only two counters are kept.
08

Maximum Number of Vowels in a Substring FAQ

How is this related to Maximum Average Subarray I?

With 1 for a vowel and 0 for any other letter, the string becomes an array and the question becomes the largest sum of k neighbouring numbers. That is Maximum Average Subarray I without the final division, and the same one-in, one-out update solves both.

What if the string could contain capital letters?

LeetCode guarantees lowercase letters only. For mixed case, test the lowercase form of each letter, or put the capital vowels in the set too; otherwise A and E count as consonants.

Does the early return change the worst case?

No. If no window is all vowels, the loop still visits every letter, so the time stays O(n). The return only helps on inputs with a long run of vowels near the start, such as a string that opens with k vowels in a row.