Maximum Vowels In Substring Length K
This problem asks you to look at every substring with length k and count how many vowels appear inside each one. Your answer should be the largest vowel count you can find among all those same-sized substrings.
Only vowels count here: a, e, i, o, and u. Every other letter should be ignored when you count. The substring also has to stay continuous, which means the letters must sit next to each other in the original word.
For example, in "abciiidef" with k = 3, one substring is "iii", and it has 3 vowels, so the answer is 3. In "aeiou" with k = 2, every length-2 piece has two vowels, so the answer is 2. If the string is "rhythm" and k = 2, then the answer is 0 because none of the windows contain vowels.
The important part is that you return the biggest vowel count, not the substring itself. You are measuring which window of length k is the most vowel-rich.
Example Input & Output
Substring "iii" contains 3 vowels.
Any length-2 window has 2 vowels.
Best window has 2 vowels.
Algorithm Flow

Solution Approach
This is a classic fixed-size sliding window problem. We are not allowed to choose any substring length we want. Every candidate window must have exactly k characters, so the efficient strategy is to maintain the vowel count for one window and update it as the window slides by one character at a time.
The first step is to define a quick way to recognize vowels, for example with a set like {'a', 'e', 'i', 'o', 'u'}. Then count how many vowels appear in the first window of length k. That gives us the starting value for both the current window and the best answer so far.
After that, slide the window from left to right. Every time the window moves one step, exactly one character leaves from the left side and exactly one new character enters from the right side. So instead of recounting the whole window, we update the count in constant time.
If the outgoing character is a vowel, subtract 1. If the incoming character is a vowel, add 1. Then compare the updated count with the best answer seen so far.
This is the main sliding-window advantage. A brute-force solution would examine every substring of size k and count vowels from scratch, which repeats a lot of work. The sliding-window version reuses the previous count and only adjusts for the two changed characters.
There is also a nice early upper bound here: the answer can never be larger than k, because a window of size k cannot contain more than k vowels. So if you ever reach a count of k, you already know it is the best possible answer.
The runtime is O(n) because each character is processed a constant number of times, and the extra space is O(1). The key insight is that adjacent fixed-length windows overlap heavily, so we should update the count instead of recomputing it.
Best Answers
class Solution {
private boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
public int maximum_vowels_in_substring_length_k(String s, int k) {
int n = s.length();
if (k <= 0 || k > n) return 0;
int window = 0;
for (int i = 0; i < k; i++) if (isVowel(s.charAt(i))) window++;
int best = window;
for (int i = k; i < n; i++) {
if (isVowel(s.charAt(i))) window++;
if (isVowel(s.charAt(i-k))) window--;
if (window > best) best = window;
}
return best;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
