Count the Number of Vowel Strings in Range — LeetCode 2586 Python Solution

EasyArrayStringCounting
Problem
#2586
Pattern
Hash Map
Reading time
2 min

The problem

You are given a 0-indexed array of string words and two integers left and right. A string is called a vowel string if it starts with a vowel character and ends with a vowel character where vowel characters are 'a', 'e', 'i', 'o', and 'u'.

Example

Input
words = ["are","amy","u"], left = 0, right = 2
Output
2
Explanation
- "are" is a vowel string because it starts with 'a' and ends with 'e'.

Python solution

Python
class Solution:
    def vowelStrings(self, words: List[str], left: int, right: int) -> int:
        return sum(
            w[0] in 'aeiou' and w[-1] in 'aeiou' for w in words[left : right + 1]
        )

Complexity

MeasureComplexity
TimeO(m)
SpaceO(1) auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2586. Count the Number of Vowel Strings in Range is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Counting.

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 2586. Count the Number of Vowel Strings in Range?
LeetCode 2586. Count the Number of Vowel Strings in Range is rated Easy on LeetCode.
What is the time complexity of LeetCode 2586. Count the Number of Vowel Strings in Range?
The Python solution on this page runs in O(m).
What is the space complexity of LeetCode 2586. Count the Number of Vowel Strings in Range?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2586. Count the Number of Vowel Strings in Range cover?
LeetCode 2586. Count the Number of Vowel Strings in Range is tagged Array, String and Counting 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