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
.
Input: word = "aeiouu" Output: 2 Explanation: The vowel substrings of word are as follows (underlined): - "aeiouu" - "aeiouu"
Input: word = "unicornarihan" Output: 0 Explanation: Not all 5 vowels are present, so there are no vowel substrings.
Input: word = "cuaieuouac" Output: 7 Explanation: The vowel substrings of word are as follows (underlined): - "cuaieuouac" - "cuaieuouac" - "cuaieuouac" - "cuaieuouac" - "cuaieuouac" - "cuaieuouac" - "cuaieuouac"
1 <= word.length <= 100
word
consists of lowercase English letters only.
class Solution:
def countVowelSubstrings(self, word: str) -> int:
ret = 0
for i in range(len(word)):
aeiou = [False] * 5
for j in range(i, len(word)):
if word[j] not in "aeiou":
break
aeiou[0] |= word[j] == 'a'
aeiou[1] |= word[j] == 'e'
aeiou[2] |= word[j] == 'i'
aeiou[3] |= word[j] == 'o'
aeiou[4] |= word[j] == 'u'
if all(aeiou):
ret += 1
return ret