Count Vowel Substrings of a String
Easy
Topics
A vowel substring is a contiguous, non-empty piece of s that consists only of vowels and contains all five of a, e, i, o and u.
Return the number of vowel substrings of s. Two pieces at different positions count separately even if they read the same.
Example 1
Input: s = "aeiouu" Output: 2 Explanation: The pieces "aeiou" and "aeiouu" both qualify.
Example 2
Input: s = "unicornarihan" Output: 0 Explanation: Every run of vowels here is broken by a consonant before all five can appear.
Example 3
Input: s = "cuaieuouac" Output: 7 Explanation: All seven lie inside the run "uaieuoua"; the consonants at each end can never be part of one.
Constraints
- 1 <= s.length <= 100
- s consists of lowercase English letters only
Editorial — intuition, approach, complexity and pitfalls
Intuition
The two conditions pull in opposite directions and that is what makes the problem easy once seen. "Only vowels" means a qualifying piece can never cross a consonant, so the string breaks into independent runs of vowels and the answer is the sum over those runs. "All five vowels" is then a question asked inside a single run.
Within a run, the piece ending at a given position has a longest valid start: push the start forward while all five vowels are still present, and every start at or before that point also works. So the count of valid pieces ending here is just the number of allowed starts, which a shrinking window gives directly.
At the input sizes here a straightforward double loop over starts and ends is also fine, and the editorial below takes it for clarity; the window version is what the problem is really probing for.
Approach
- Walk the string, splitting it at consonants into maximal runs of vowels.
- For each run, consider every start and extend the end, tracking which of the five vowels the piece currently holds.
- Count a piece as soon as all five are present, and keep counting as the end advances — adding more vowels never invalidates it.
- Sum the counts over all runs.
Walkthrough
s = "cuaieuouac"
consonants at index 0 and 9, so one run: "uaieuoua"
indices within the run: u a i e u o u a
0 1 2 3 4 5 6 7
The only o is at 5, so every valid piece must contain index
5; the only i is at 2 and the only e at 3, so every valid
piece must start at or before 2.
start 0: ends 5, 6, 7 -> 3 pieces
start 1: ends 5, 6, 7 -> 3 pieces (u at 4 supplies the u)
start 2: end 7 only -> 1 piece (needs the a at 7)
start 3+: no i left -> 0
3 + 3 + 1 = 7Complexity
- Time — O(n^2): every start extends until it meets a consonant. The sliding-window version is O(n), which matters at larger limits than this problem sets.
- Space — O(1): the set of seen vowels holds at most five entries.
Common Pitfalls
- Counting pieces that contain all five vowels but also a consonant. The piece must be vowels and nothing else.
- Breaking out of the inner loop once all five vowels are found. Extending further still gives valid pieces — "aeiouu" counts as well as "aeiou".
- Requiring each vowel exactly once. Repeats are allowed, which is the whole of example 1.
- Counting distinct substrings rather than positions. Two identical pieces at different offsets are two answers.
Full Solution — all languages
class Solution:
def countVowelSubstrings(self, s):
VOWELS = set("aeiou")
total = 0
for start in range(len(s)):
if s[start] not in VOWELS:
continue
seen = set()
for end in range(start, len(s)):
if s[end] not in VOWELS:
break
seen.add(s[end])
if len(seen) == 5:
total += 1
return totalimport java.util.*;
class Solution {
public int countVowelSubstrings(String s) {
String vowels = "aeiou";
int total = 0;
for (int start = 0; start < s.length(); start++) {
if (vowels.indexOf(s.charAt(start)) < 0) continue;
Set<Character> seen = new HashSet<>();
for (int end = start; end < s.length(); end++) {
if (vowels.indexOf(s.charAt(end)) < 0) break;
seen.add(s.charAt(end));
if (seen.size() == 5) total++;
}
}
return total;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int countVowelSubstrings(string s) {
auto isVowel = [](char c) {
return string("aeiou").find(c) != string::npos;
};
int total = 0;
for (size_t start = 0; start < s.size(); start++) {
if (!isVowel(s[start])) continue;
set<char> seen;
for (size_t end = start; end < s.size(); end++) {
if (!isVowel(s[end])) break;
seen.insert(s[end]);
if (seen.size() == 5) total++;
}
}
return total;
}
};/**
* @param {string} s
* @return {number}
*/ var countVowelSubstrings = function(s) {
const isVowel = (c) => "aeiou".indexOf(c) >= 0;
let total = 0;
for (let start = 0; start < s.length; start++) {
if (!isVowel(s[start])) continue;
const seen = new Set();
for (let end = start; end < s.length; end++) {
if (!isVowel(s[end])) break;
seen.add(s[end]);
if (seen.size === 5) total++;
}
}
return total;
};