← Visualizers
Sliding Window · fixed-size window LeetCode 1456

Maximum Number of Vowels in a Substring of Given Length / visualized

The window is exactly k wide and stays exactly k wide — there is no shrink decision to make here, so the only design question is what statistic rides along with the frame. Here it is a single integer: how many vowels are inside. Each slide is two O(1) edits, count += isVowel(s[i]) for the character entering on the right and count -= isVowel(s[i-k]) for the one falling off the left, which makes the whole scan O(n). Recounting each window from scratch would be O(n·k) for the same answer. And since a window of width k has only k slots, the moment the count reaches k you are done — nothing later can beat it.

Execution

idle
s  ·  dotted cells are vowels
Press Run to begin.
0 / 0
Speed

Java · running line

inside the window entering (count +=) leaving (count -=) best window so far slid past
O(n) time · O(1) space  ·  the width is fixed, so a slide is one add and one drop — never a recount. best == k is a hard ceiling: exit early.