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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach means we're going to look at every possible group of letters inside the given word. We'll check each group to see if it has only vowels and if it contains all the vowels at least once.
Here's how the algorithm would work step-by-step:
def count_vowel_substrings_brute_force(word):
number_of_vowel_substrings = 0
word_length = len(word)
for start_index in range(word_length):
for end_index in range(start_index, word_length):
substring = word[start_index:end_index + 1]
# Verify only vowels are in the current substring
if all(char in 'aeiou' for char in substring):
vowels_present = set()
for char in substring:
vowels_present.add(char)
# Check if all vowels are present in substring
if len(vowels_present) == 5:
number_of_vowel_substrings += 1
return number_of_vowel_substrings
The best way to count vowel substrings is to avoid checking every possible substring. Instead, we cleverly check each possible starting position and extend the substring only as long as it contains all the required vowels, stopping early when the conditions are not met.
Here's how the algorithm would work step-by-step:
def count_vowel_substrings(word):
substring_count = 0
word_length = len(word)
for start_index in range(word_length):
vowel_counts = {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}
for end_index in range(start_index, word_length):
current_character = word[end_index]
if current_character in 'aeiou':
vowel_counts[current_character] += 1
# Check if all vowels are present in the substring.
if all(vowel_counts[vowel] > 0 for vowel in 'aeiou'):
substring_count += 1
else:
# Stop if a non-vowel is encountered
break
return substring_count
Case | How to Handle |
---|---|
Empty input string | Return 0 immediately as there are no substrings. |
String with length less than 5 | Return 0, as at least 5 characters are needed to potentially contain all five vowels. |
String contains non-vowel characters | Ignore substrings containing non-vowels as they cannot be vowel substrings; only consider contiguous sequences of vowels. |
String with only one vowel type repeated | Return 0 as it's impossible to have all five vowels if only one vowel exists. |
String where no substring contains all five vowels | The solution should correctly iterate and return 0 if no valid substring is found. |
String with very long length | Ensure the algorithm has reasonable time complexity (e.g., O(n^2) or better) to avoid timeouts. |
String containing all vowels but not contiguously | Only count contiguous substrings, not arrangements of vowels separated by consonants. |
String with multiple occurrences of all five vowels | The solution should count all valid substrings, even if they overlap or are contained within each other. |