We need to look at every substring of length exactly k in the string s and count how many vowels each one contains. Then we return the highest count we find.
A substring is a contiguous block of characters. So for s = "abciiidef" and k = 3, the substrings are: "abc", "bci", "cii", "iii", "iid", "ide", "def". We count the vowels in each and take the max.
Consecutive substrings of length k overlap heavily. Moving from one substring to the next loses one character on the left and gains one character on the right, while everything in between stays the same. Instead of recounting vowels from scratch every time, we can adjust the count using only the character we lost and the character we gained. That is the sliding window idea, and it turns a repetitive counting problem into a single pass over the string.
1 <= s.length <= 10^5 → With n up to 100,000, we need O(n) or O(n log n). An O(n * k) approach where k is close to n would be O(n^2), which is too slow.s consists of lowercase English letters → We only need to check if a character is one of 5 vowels. A set lookup or simple conditional works.1 <= k <= s.length → k is always valid. We don't need to handle k > s.length. But k could equal s.length, meaning there's only one substring to check.For every starting position, extract the substring of length k, count the vowels in it, and track the maximum. This mirrors how you would solve it by hand: pick a window, count the vowels, move to the next window, count again.
There are n - k + 1 substrings of length k, and counting vowels in each takes O(k) time, so the total work is O(n * k). When k is small this is fast, but when k is close to n it becomes O(n^2), which is too slow for n up to 100,000.
maxVowels = 0.i from 0 to n - k:i to i + k - 1.maxVowels if this count is larger.maxVowels.Loading animation...
This approach recounts the entire window from scratch each time. Consecutive windows share k-1 characters, so most of that counting is repeated. The next approach reuses the previous count and adjusts only for the two characters that change.
When the window slides one position to the right, the substring changes by exactly two characters: the character at the left edge drops out, and a new character at the right edge comes in. The k-1 characters in the middle stay the same.
If we already know how many vowels are in the current window, we can compute the vowel count for the next window in O(1). Check whether the outgoing character was a vowel (subtract 1 if so) and whether the incoming character is a vowel (add 1 if so).
The full algorithm becomes: count the vowels in the first window of size k, then slide across the string making O(1) adjustments at each step. This runs in O(n) time.
The window maintains one invariant: currentCount always equals the number of vowels in the current window of size k. When the window at iteration i ends at index i, it spans indices i - k + 1 to i. The character at index i - k left the window on this step, so subtracting its vowel contribution removes a count that no longer belongs. The character at index i entered on this step, so adding its contribution accounts for it. The k-1 characters between them belonged to both the old and new windows, so their contribution carries over unchanged. The invariant therefore holds after every slide, and maxVowels records the largest value it ever took. Each character is added once and subtracted once across the whole traversal, so the total work is O(n).
currentCount and maxVowels.i - k) is a vowel, decrement currentCount.i) is a vowel, increment currentCount.maxVowels if currentCount is larger.maxVowels.Loading animation...