2062. Count Vowel Substrings of a String LeetCode Solution
In this guide, you will get 2062. Count Vowel Substrings of a String LeetCode Solution with the best time and space complexity. The solution to Count Vowel Substrings of a String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Vowel Substrings of a String solution in C++
- Count Vowel Substrings of a String solution in Java
- Count Vowel Substrings of a String solution in Python
- Additional Resources

Problem Statement of Count Vowel Substrings of a String
A substring is a contiguous (non-empty) sequence of characters within a string.
A vowel substring is a substring that only consists of vowels (‘a’, ‘e’, ‘i’, ‘o’, and ‘u’) and has all five vowels present in it.
Given a string word, return the number of vowel substrings in word.
Example 1:
Input: word = “aeiouu”
Output: 2
Explanation: The vowel substrings of word are as follows (underlined):
– “aeiouu”
– “aeiouu”
Example 2:
Input: word = “unicornarihan”
Output: 0
Explanation: Not all 5 vowels are present, so there are no vowel substrings.
Example 3:
Input: word = “cuaieuouac”
Output: 7
Explanation: The vowel substrings of word are as follows (underlined):
– “cuaieuouac”
– “cuaieuouac”
– “cuaieuouac”
– “cuaieuouac”
– “cuaieuouac”
– “cuaieuouac”
– “cuaieuouac”
Constraints:
1 <= word.length <= 100
word consists of lowercase English letters only.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(26) = O(1)
2062. Count Vowel Substrings of a String LeetCode Solution in C++
class Solution {
public:
int countVowelSubstrings(string word) {
return countVowelSubstringsAtMost(word, 5) -
countVowelSubstringsAtMost(word, 4);
}
private:
int countVowelSubstringsAtMost(const string& s, int goal) {
int ans = 0;
int k = goal;
vector<int> count(26);
for (int l = 0, r = 0; r < s.length(); ++r) {
if (!isVowel(s[r])) { // Fresh start.
l = r + 1;
k = goal;
count = vector<int>(26);
continue;
}
if (++count[s[r] - 'a'] == 1)
--k;
while (k == -1)
if (--count[s[l++] - 'a'] == 0)
++k;
ans += r - l + 1; // s[l..r], s[l + 1..r], ..., s[r]
}
return ans;
}
bool isVowel(char c) {
static constexpr string_view kVowels = "aeiou";
return kVowels.find(c) != string_view::npos;
}
};
/* code provided by PROGIEZ */
2062. Count Vowel Substrings of a String LeetCode Solution in Java
class Solution {
public int countVowelSubstrings(String word) {
return countVowelSubstringsAtMost(word, 5) - countVowelSubstringsAtMost(word, 4);
}
private int countVowelSubstringsAtMost(final String s, int goal) {
int ans = 0;
int k = goal;
int[] count = new int[26];
for (int l = 0, r = 0; r < s.length(); ++r) {
if (!isVowel(s.charAt(r))) { // Fresh start.
l = r + 1;
k = goal;
count = new int[26];
continue;
}
if (++count[s.charAt(r) - 'a'] == 1)
--k;
while (k == -1)
if (--count[s.charAt(l++) - 'a'] == 0)
++k;
ans += r - l + 1; // s[l..r], s[l + 1..r], ..., s[r]
}
return ans;
}
private boolean isVowel(char c) {
return "aeiou".indexOf(c) != -1;
}
}
// code provided by PROGIEZ
2062. Count Vowel Substrings of a String LeetCode Solution in Python
class Solution:
def countVowelSubstrings(self, word: str) -> int:
kVowels = 'aeiou'
def countVowelSubstringsAtMost(goal: int) -> int:
ans = 0
k = goal
count = collections.Counter()
l = 0
for r, c in enumerate(word):
if c not in kVowels: # Fresh start.
l = r + 1
k = goal
count = collections.Counter()
continue
count[c] += 1
if count[c] == 1:
k -= 1
while k == -1:
count[word[l]] -= 1
if count[word[l]] == 0:
k += 1
l += 1
ans += r - l + 1 # s[l..r], s[l + 1..r], ..., s[r]
return ans
return countVowelSubstringsAtMost(5) - countVowelSubstringsAtMost(4)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.