Date: 2026-06-06
Time: 16:06
count-vowel-substrings-of-a-string/solution.pySolves 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.
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.
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.
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).
1. Build the reference vowel set {'a', 'e', 'i', 'o', 'u'}.
2. For each start position i in [0, n):
seen to empty.j in [i, n):word[j] is a consonant → break (no further extension is valid).word[j] to seen.seen has all 5 vowels → increment count (and keep going — longer substrings may also qualify).3. Return count.
seen is a subset of vowels at all times within the inner loop — enforced by the break on non-vowel characters.len(seen) == 5, every subsequent iteration of the inner loop also increments count — because seen only grows (characters are added, never removed), and the loop only continues through vowels.None. The function assumes well-formed input (lowercase English letters). Empty strings naturally return 0 because the outer range(n) produces no iterations.