Count Vowel Substrings of a String — LeetCode 2062 Python Solution

EasyHash TableString
Problem
#2062
Pattern
Hash Map
Reading time
2 min

The problem

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.

Example

Input
word = "aeiouu"
Output
2
Explanation
The vowel substrings of word are as follows (underlined):

Python solution

Python
class Solution:
    def countVowelSubstrings(self, word: str) -> int:
        s = set("aeiou")
        ans, n = 0, len(word)
        for i in range(n):
            t = set()
            for c in word[i:]:
                if c not in s:
                    break
                t.add(c)
                ans += len(t) == 5
        return ans

Complexity

MeasureComplexity
TimeO(n^2)
SpaceO(C) auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2062. Count Vowel Substrings of a String is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2062. Count Vowel Substrings of a String?
LeetCode 2062. Count Vowel Substrings of a String is rated Easy on LeetCode.
What is the time complexity of LeetCode 2062. Count Vowel Substrings of a String?
The Python solution on this page runs in O(n^2).
What is the space complexity of LeetCode 2062. Count Vowel Substrings of a String?
The Python solution on this page uses O(C) auxiliary space.
What topics does LeetCode 2062. Count Vowel Substrings of a String cover?
LeetCode 2062. Count Vowel Substrings of a String is tagged Hash Table and String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview