Sliding Window· 25 min
Maximum Number of Vowels in a Substring of Given Length
LeetCode #1456 · Medium · Interactive step-by-step visualization
Setup
Example input
s = "abciiidef", k = 3Step 1 / 28
Current step
1 / 28Build the first fixed window. Exactly 3 contiguous items belong to each candidate. Vowel count starts at 1.
Step log
s = "abciiidef", k = 3
CURRENT WINDOW
a
0b
1c
2i
3i
4i
5d
6e
7f
8Current dotted windowSaved best range
current
1
best
1
Active cursor
Comparing
Best or completed
Leaving or removed
maximum-number-of-vowels-in-a-substring-of-given-length.java
1public int maxVowels(String s, int k) {2 int count = 0;3 for (int i = 0; i < k; i++) if (isVowel(s.charAt(i))) count++;4 int best = count;5 for (int right = k; right < s.length(); right++) {6 if (isVowel(s.charAt(right))) count++;7 if (isVowel(s.charAt(right - k))) count--;8 best = Math.max(best, count);9 }10 return best;11}● Java
Time
O(n)
Space
O(1)
Pattern
sliding window
Frames
28
