Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Sliding window

Most vowels in any k letters

Easy · Target 10 minutes · Sliding window· Type asked at

Given a lowercase string s and an integer k, return the largest number of vowels (a, e, i, o, u) in any substring of length k.

Examples

InputOutputWhy
s = "abciiidef", k = 33"iii".
s = "aeiou", k = 22Every window is all vowels.
s = "rhythm", k = 20No vowels at all.

Constraints

  • 1 ≤ k ≤ |s| ≤ 10⁵
  • s is lowercase English letters

Hints

Hint 1

This is the running-sum window with a different quantity: count vowels instead of adding values.

Hint 2

When the window moves, only two letters change: the one entering and the one leaving.

Hint 3

The answer can never exceed k, so you may stop early once you reach it.

Where people lose marks

  • Recounting the whole window at each step, which is O(nk).
  • Checking the entering letter but forgetting to remove the leaving one.
Console⌘/Ctrl + Enter runs

Write a solution and run it against the real test table.


Keep going

Preparing for a real process? Quant interview preparation, or book a free 20-minute call.