mediumSliding WindowSliding Window

Maximum Number of Vowels in a Substring of Given Length

mediumTime: O(n)Space: O(1)

Recognize the pattern

max vowels in any substring of length kfixed-size windowcount vowels entering/leaving

Brute force idea

For each window count vowels — O(nk).

Better approach

Sliding window: maintain vowel count. +1 if entering char is vowel, -1 if leaving char is vowel. Track max. O(n).

Key invariant

Vowel count updates in O(1) per slide. Never recount the whole window.

Watch out for

Recounting each window — update incrementally.

Sliding Window Pattern