File: count-vowel-substrings-of-a-string/solution.py

Date: 2026-06-06

Time: 16:06

count-vowel-substrings-of-a-string/solution.py

Purpose

Solves LeetCode 2062: Count Vowel Substrings of a String. The file exports a single function that counts substrings which are composed entirely of vowels and contain all five vowels (a, e, i, o, u). It's a standalone solution module — no imports, no classes, just one pure function.

Key Components

countvowelsubstrings(word: str) -> int — The sole entry point. Takes a lowercase English string, returns the count of qualifying substrings.

The function enforces a conjunction of two constraints on each substring:

1. Every character must be a vowel (no consonants allowed).

2. All five vowels must appear at least once.

Patterns

Brute-force enumeration with early termination. The outer loop picks every possible start index i. The inner loop extends the substring rightward from i. The break on line 15 is the key optimization — the moment a consonant is hit, all longer substrings starting at i are guaranteed to also contain that consonant, so the inner loop terminates early.

Accumulator set (seen). Rather than re-scanning each substring to check for all five vowels, the code incrementally builds a set of distinct vowels seen so far. This turns the "contains all five vowels" check into a constant-time len(seen) == 5 comparison.

This is an O(n²) worst-case solution (when the entire string is vowels), which is fine for the problem's constraint of n ≤ 100.

Dependencies

Imports: None. Pure Python with only set from builtins.

Imported by: count-vowel-substrings-of-a-string/test_solution.py (and incidentally listed alongside hundreds of other test files in the "Imported By" section — that list reflects the repo's test harness structure, not actual usage of this function by other solutions).

Flow

1. Build the reference vowel set {'a', 'e', 'i', 'o', 'u'}.

2. For each start position i in [0, n):

3. Return count.

Invariants

Error Handling

None. The function assumes well-formed input (lowercase English letters). Empty strings naturally return 0 because the outer range(n) produces no iterations.