Maximum Number of Vowels in a Substring of Given Length

Medium Arrays & Hashing Sliding Window · Fixed Original on LeetCode

Maximum Number of Vowels in a Substring of Given Length is a medium arrays & hashing problem solved with the sliding window · fixed pattern. The best approach, optimal (fixed sliding window), runs in O(n) time and O(1) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force up.

Problem

Given a lowercase string s and an integer k, return the largest number of vowels in any substring of s of length exactly k.

Examples

Example 1

Input
s = "leetcode", k = 3
Output
2
Why
"lee", "eet" and "ode" each contain 2 vowels.

Example 2

Input
s = "rhythm", k = 2
Output
0

Constraints

  • 1 <= k <= s.length <= 10^5
  • s is lowercase English letters; vowels are a, e, i, o, u.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Brute forceO(n · k)O(1)
Optimal (fixed sliding window)O(n)O(1)

1Brute force

TimeO(n · k)
SpaceO(1)

Count vowels in each window of size k from scratch.

  1. For each start i, count vowels in s[i..i+k-1].
class Solution {
    public int maxVowels(String s, int k) {
        int best = 0;
        for (int i = 0; i + k <= s.length(); i++) {
            int c = 0;
            for (int j = i; j < i + k; j++) if (isVowel(s.charAt(j))) c++;
            best = Math.max(best, c);
        }
        return best;
    }

    private boolean isVowel(char ch) { return "aeiou".indexOf(ch) >= 0; }
}

2Optimal (fixed sliding window)

TimeO(n)
SpaceO(1)

Keep the vowel count of the current window. Add 1 when a vowel enters on the right and subtract 1 when a vowel leaves on the left.

  1. Add each character; once i >= k, remove s[i - k].
  2. After the window reaches size k, update best.
class Solution {
    public int maxVowels(String s, int k) {
        int count = 0, best = 0;
        for (int i = 0; i < s.length(); i++) {
            if (isVowel(s.charAt(i))) count++;
            if (i >= k && isVowel(s.charAt(i - k))) count--;
            if (i >= k - 1) best = Math.max(best, count);
            if (best == k) break;
        }
        return best;
    }

    private boolean isVowel(char ch) { return "aeiou".indexOf(ch) >= 0; }
}

Edge cases to test

  • No vowels at all
  • The answer reaches k, so you can stop early

Hints

Hint 1

Count vowels in the first window, then update the count by one character in and one out.

FAQ

What is the best time complexity for Maximum Number of Vowels in a Substring of Given Length?

Optimal (fixed sliding window) runs in O(n) time and O(1) extra space.

Which pattern does Maximum Number of Vowels in a Substring of Given Length use?

It is a arrays & hashing problem that uses the sliding window · fixed pattern. Other problems with the same pattern: Maximum Average Subarray I, K Radius Subarray Averages, Maximum Points You Can Obtain from Cards.

Is there a brute force solution for Maximum Number of Vowels in a Substring of Given Length?

Yes. Brute force takes O(n · k) time and O(1) space. Count vowels in each window of size k from scratch.

Which edge cases should I test for Maximum Number of Vowels in a Substring of Given Length?

No vowels at all; The answer reaches k, so you can stop early.