Sliding Window· 25 min

Maximum Number of Vowels in a Substring of Given Length

LeetCode #1456 · Medium · Interactive step-by-step visualization

Setup

Example inputs = "abciiidef", k = 3
Step 1 / 28

Current step

1 / 28

Build 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
0
b
1
c
2
i
3
i
4
i
5
d
6
e
7
f
8
Current 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