Skip to content

2062. Count Vowel Substrings of a String

Difficulty Topics

Description

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.

Solution

count-vowel-substrings-of-a-string.py
class Solution:
    def countVowelSubstrings(self, word: str) -> int:
        res = 0

        n = len(word)

        for i in range(n):
            A = [0, 0, 0, 0, 0]
            for j in range(i, n):
                if word[j] == 'a':
                    A[0] += 1
                elif word[j] == 'e':
                    A[1] += 1
                elif word[j] == 'i':
                    A[2] += 1
                elif word[j] == 'o':
                    A[3] += 1
                elif word[j] == 'u':
                    A[4] += 1
                else:
                    break

                if all(x > 0 for x in A):
                    res += 1

        return res