Java String Program · S48
Maximum Number of Vowels in a Substring in Java
Look at every window of k consecutive characters and count its vowels. Sliding the window by one character changes the count by at most one at each end.
Problem Statement
Given a string of lowercase letters and an integer k, return the largest number of vowels (a, e, i, o, u) in any substring of length exactly k.
You may assume that 1 ≤ k ≤ s.length().
Input/Output Examples
"javabytechie", k = 3Output2Explanation: windows such as "ava" and "hie" contain 2 vowels; none has 3."rhythm", k = 2Output0Explanation: the string has no vowels at all."aeiou", k = 2Output2Explanation: every window is two vowels.Visual Explanation
"jav" to "ava": the vowel a enters on the right and the consonant j leaves on the left, so the count goes from 1 to 2.Each slide adds one character and removes one, so the vowel count changes by at most one in each direction. Updating it costs O(1) instead of recounting k characters.
Approaches
The sliding window updates one count. The prefix-sum version precomputes counts so that any window can be measured with a subtraction.
Approach 1: Fixed Sliding Window (Recommended)
Idea: add 1 when a vowel enters the window and subtract 1 when a vowel leaves it, tracking the best count.
- For each index
i, add 1 ifs[i]is a vowel. - If
i ≥ k, subtract 1 ifs[i - k], the character leaving, is a vowel. - Once the first full window is reached, update the best count.
Time: O(n) · Space: O(1).
Approach 2: Prefix Sums
Idea: store the number of vowels in every prefix; the vowels in s[start..start + k - 1] are prefix[start + k] - prefix[start].
- Build
prefixof lengthn + 1. - For every start position, compute the window count by subtraction.
- Return the largest count.
Time: O(n) · Space: O(n).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Fixed Sliding Window (Recommended)
One pass and two variables. The check i ≥ k - 1 makes sure only complete windows are compared.
public class VowelWindow {
// Fixed window of size k: add the entering character, remove the leaving one
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++;
}
if (i >= k && isVowel(s.charAt(i - k))) {
count--;
}
if (i >= k - 1) {
best = Math.max(best, count);
}
}
return best;
}
private static boolean isVowel(char c) {
return "aeiou".indexOf(c) >= 0;
}
public static void main(String[] args) {
Object[][] cases = {{"javabytechie", 3}, {"rhythm", 2}, {"aeiou", 2}};
for (Object[] c : cases) {
System.out.println("\"" + c[0] + "\", k=" + c[1] + " -> " + maxVowels((String) c[0], (Integer) c[1]));
}
}
}
"javabytechie", k=3 -> 2 "rhythm", k=2 -> 0 "aeiou", k=2 -> 2
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Prefix Sums
Uses more memory, but answers any window query in constant time, which helps when there are many different values of k.
public class VowelPrefix {
// prefix[i] = vowels in s[0..i-1]; any window's count is a difference of two prefixes
static int maxVowels(String s, int k) {
int n = s.length();
int[] prefix = new int[n + 1];
for (int i = 0; i < n; i++) {
prefix[i + 1] = prefix[i] + ("aeiou".indexOf(s.charAt(i)) >= 0 ? 1 : 0);
}
int best = 0;
for (int start = 0; start + k <= n; start++) {
best = Math.max(best, prefix[start + k] - prefix[start]);
}
return best;
}
public static void main(String[] args) {
Object[][] cases = {{"javabytechie", 3}, {"rhythm", 2}, {"aeiou", 2}};
for (Object[] c : cases) {
System.out.println("\"" + c[0] + "\", k=" + c[1] + " -> " + maxVowels((String) c[0], (Integer) c[1]));
}
}
}
"javabytechie", k=3 -> 2 "rhythm", k=2 -> 0 "aeiou", k=2 -> 2
Time complexity: O(n) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Fixed Sliding Window | O(n) | O(1) |
| Prefix Sums | O(n) | O(n) |
Here n is the length of the string. Both approaches are linear; the prefix-sum version also stores n + 1 integers.
Interview Tips and Recommended Approach
- Recognise the fixed window. Every window has the same length, so only the two ends change.
- Stop early if possible. If the count ever equals
k, no window can do better. - Know prefix sums. They turn any range-count question into a subtraction.
- Use a lookup for vowels. A
boolean[26]or"aeiou".indexOf(c)keeps the check short.
Recommended Approach: Fixed Sliding Window
Why: it scans the string once with constant extra memory, and the add-one, remove-one update is the core of every fixed-size window problem.