File: count-the-number-of-vowel-strings-in-range/solution.py

Date: 2026-06-06

Time: 16:06

Purpose

This file solves LeetCode 2586: Count the Number of Vowel Strings in Range. It counts how many strings in a subarray words[left..right] both start and end with a vowel. It's a straightforward array-scan problem classified as Easy.

Key Components

VOWELS (module-level constant) — A set of the five lowercase vowels. Using a set gives O(1) membership testing, though for a 5-element collection the practical difference from a string in check is negligible.

is_vowel(char: str) -> bool — Thin predicate wrapping the set lookup. Extracted as a named function for readability in the generator expression below.

Solution.vowelStrings(self, words, left, right) -> int — The LeetCode entry point. Iterates indices left through right inclusive, counting words where both words[i][0] and words[i][-1] are vowels. Returns the count as an int.

Patterns

Dependencies

Imports: Only typing.List for the type annotation — no external libraries.

Imported by: The testsolution.py in the same directory. The large "Imported By" list in the prompt is misleading — those are other problems' test files that import *their own* solution.py, not this one. The actual consumer is count-the-number-of-vowel-strings-in-range/testsolution.py.

Flow

1. Caller provides words, left, right.

2. A generator iterates i from left to right (inclusive).

3. For each i, it checks words[i][0] (first char) and words[i][-1] (last char) against VOWELS via is_vowel.

4. Matches contribute 1 to the running sum.

5. The total count is returned.

Time complexity: O(right - left + 1) — single pass, constant work per word.

Space complexity: O(1) — generator, no auxiliary data structures.

Invariants

Error Handling

None. The code trusts the LeetCode runtime to supply valid inputs. An empty word would raise IndexError on words[i][0]; out-of-bounds left/right would silently produce wrong results or raise IndexError. This is appropriate for a competitive-programming context.

Topics to Explore

Beliefs