Maximum Number of Vowels in a Substring of Given Length
Slide a window of k characters across a string and find the most vowels it ever holds.
Given a string sksksks
Example 1
Output: 3
Explanation: the substring "iii" has 3 vowels.
Example 2
Output: 2
Explanation: every substring of length 2 has 2 vowels.
Example 3
Output: 2
Explanation: the substring "ode" has 2 vowels.
The Idea
Picture a strip of card with a slot cut in it, exactly 3 letters wide, laid over the word "abciiidef". Through the slot you see one substring of length 3. Slide the card one letter at a time from the left end to the right end, and count the vowels you see at each stop. The answer is the biggest count.
For Example 1 the slot shows "abc" (1 vowel), then "bci" (1), "cii" (2), "iii" (3), "iid" (2), "ide" (2) and "def" (1). That is seven stops, and the best is 3. Counting every stop from scratch means reading all 3 letters again each time. Is that really needed, when two neighbouring stops share most of their letters?
Look at the picture below. The part of the string inside the slot is called the window, drawn as the blue frame. The small number under each letter is its position, counting from 0.
In the top row, labelled window "cii", the frame covers positions 2 to 4 and holds 2 vowels. In the row labelled window "iii", the frame has moved one step right. Only two letters changed: c at position 2 left, and i at position 5 entered. The two i's at positions 3 and 4 stayed where they were.
So the new count follows from the old one. The c that left was not a vowel, so nothing comes off. The i that entered is a vowel, so 1 goes on, and 2 becomes 3. Each step needs a look at only two letters, however wide the window is.
Moving a window of fixed size one step at a time, and updating what it holds instead of recomputing it, is called the sliding window technique.
Solution Approach
Turning that idea into code means settling three things: how to update the count at each step, how to build the first window, and where to keep the best count.
Updating the count at each step
Why not just count the vowels of each window again? A string of length n has n - k + 1 windows, and each one takes k reads. For n = 100,000 and k = 50,000 that is about 2.5 billion reads. Updating the count takes two reads per step instead.
Which two characters change? Call the position of the window's right end iiki - k
Building the first window
What about the start, before the window holds kiki - kik
Keeping the best count
Where does the answer come from? A variable holds the best count seen so far, and it is compared with the count after every step. The counts during the growing stage, for "a" and "ab", are never bigger than the count for "abc", which contains them, so comparing them too does no harm.
Stopping early (optional)
A window of kkk
Java Solution
The method maxVowels
- First, set
, the number of vowels in the current window, andcount , the largest count seen so far, both to 0.best - Move
, the window's right end, along the string with one loop. Ifi is a vowel, add 1 tos.charAt(i) , because that character enters the window. A small helper,count , compares a character with the five vowels.isVowel - Once
is at leasti , the character atk has just left the window. If it is a vowel, subtract 1 fromi - k .count - After both checks, update
withbest .Math.max(best, count) - When the loop ends, return
.best
The main
public class MaximumNumberOfVowelsInSubstringOfGivenLength {
public static int maxVowels(String s, int k) {
int count = 0;
int best = 0;
for (int i = 0; i < s.length(); i++) {
if (isVowel(s.charAt(i))) {
count++; // character i enters the window
}
if (i >= k && isVowel(s.charAt(i - k))) {
count--; // character i - k leaves it
}
best = Math.max(best, count);
}
return best;
}
private static boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
public static void main(String[] args) {
System.out.println(maxVowels("abciiidef", 3));
System.out.println(maxVowels("aeiou", 2));
System.out.println(maxVowels("cscode", 3));
System.out.println(maxVowels("rhythm", 2));
System.out.println(maxVowels("abe", 3));
System.out.println(maxVowels("xyzxyzaa", 2));
}
}
Running it prints:
3
2
2
0
2
2
Look at the third line, 2 for "cscode" with k = 3i - k + 1i - k
The fourth line is 0, because "rhythm" has no vowels and countk = 3k = 2best
The original implementation,
Complexity
The work is a single pass over the string, with a fixed amount of work at each step.
- Time: O(n), because the window's right end
visits each of the n characters once, and each visit checks at most two characters, the one entering and the one leaving.i - Space: O(1), because the method keeps only
,count andbest , and reads the string by index withi without copying it.charAt
Windows of fixed size are the first pattern in