#1456

Maximum Number of Vowels in a Substring of Given Length

Medium Fixed window

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the most vowels found in any substring of length k.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the most vowels in any k-length window.

Why this works: fixed window; +1 if the new letter is a vowel, -1 if the letter leaving was one.

Example: "abciiidef", k = 3

"abc" 1, "bci" 1, "cii" 2, "iii" 3 (best)

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using s.substring for each window, which copies k characters each time.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Sliding Window
🎬 Episode 10: video coming soon