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.The problem asks to count the number of vowel substrings within a given string that contain all five vowels (a, e, i, o, u). The solutions presented use different approaches to achieve this.
Solution 1 (More Efficient): This solution iterates through all possible substrings and checks if they satisfy the conditions.
i
of the substring.j
of the substring starting from i
.set
(or HashSet
in Java, unordered_set
in C++) is used to efficiently track the unique vowels present in the current substring. The set
only contains vowels encountered so far in the current substring. If the size of the set becomes 5, it means all five vowels are present, and the counter ans
is incremented.Solution 2 (Slightly Less Efficient): This solution is similar but uses a slightly different approach to check for vowel substring.
i
.i
to the end of the string.ans
is incremented.Time Complexity Analysis:
Both solutions have a time complexity of O(n^2), where n is the length of the input string. The nested loops dominate the runtime. In the worst-case scenario (a string with many vowels), the inner loop will iterate almost to the end of the string for many iterations of the outer loop.
Space Complexity Analysis:
The space complexity is O(1) for both solutions. The space used by the set
(or equivalent data structure) remains constant, regardless of the input string length. The size of the set is always bounded by the number of vowels (5).
Code Examples (Python, Java, C++, Go, TypeScript):
The provided code examples demonstrate the implementation of both solutions in various programming languages. They follow the algorithmic descriptions outlined above. Note that Solution 1 is generally preferred due to its slightly more efficient handling of substrings, especially for larger input strings. The difference may not be noticeable for smaller inputs.
Example (Python - Solution 1):
class Solution:
def countVowelSubstrings(self, word: str) -> int:
n = len(word)
s = set('aeiou')
return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
This concise Python code leverages generator expressions for a compact and efficient implementation of Solution 1.
The other languages' code examples are similarly straightforward implementations of the described algorithms. They all share the same core logic but adapt to the specific syntax and data structures of each language.