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.
scan be 10⁵ letters long andkcan be just as large, so counting every window letter by letter is too slow.
- 1 <= s.length <= 10⁵
- s consists of lowercase English letters.
- 1 <= k <= s.length
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 ofkletters cannot hold more thankvowels. Once some window is all vowels, nothing later can beat it, so the scan can stop there.
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.
Approach
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.
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.
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.
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.
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.
Code in Python, C++ and Java
Common pitfalls
Dropping the wrong letter
count -= s[i - k + 1] in vowels
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
count = 0
for i in range(len(s)):
count += s[i] in vowels
count -= s[i - k] in vowelscount = 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.
Edge cases
k equals the length of sThe 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 letter ever adds 1, so count and best stay 0 and the answer is 0.
Complexity
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.