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'.
Return the number of vowel strings words[i] where i belongs to the inclusive range [left, right].
Example 1:
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'. - "amy" is not a vowel string because it does not end with a vowel. - "u" is a vowel string because it starts with 'u' and ends with 'u'. The number of vowel strings in the mentioned range is 2.
Example 2:
Input: words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4 Output: 3 Explanation: - "aeo" is a vowel string because it starts with 'a' and ends with 'o'. - "mu" is not a vowel string because it does not start with a vowel. - "ooo" is a vowel string because it starts with 'o' and ends with 'o'. - "artro" is a vowel string because it starts with 'a' and ends with 'o'. The number of vowel strings in the mentioned range is 3.
Constraints:
1 <= words.length <= 10001 <= words[i].length <= 10words[i] consists of only lowercase English letters.0 <= left <= right < words.lengthIn #2586 Count the Number of Vowel Strings in Range, you are given an array of words and two indices defining a range. The task is to count how many strings within this range both start and end with a vowel. Vowels typically include a, e, i, o, u.
A straightforward approach is to iterate through the array from left to right. For each word, check whether the first and last characters belong to a predefined vowel set. If both characters are vowels, increment a counter. Using a set or hash lookup for vowels ensures constant-time checks.
This problem mainly tests careful iteration and string boundary checks. Since each word is processed once within the given range, the approach is efficient and easy to implement. The overall complexity depends on the number of words examined in the range.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Iterate through range and check first/last characters | O(n) | O(1) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
consider iterating over all strings from left to right and use an if condition to check if the first character and last character are vowels.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, if multiple queries over different ranges are required, a prefix sum array can store counts of valid vowel strings up to each index. This allows each query to be answered in constant time after preprocessing.
While this exact problem may not frequently appear, similar string filtering and counting problems are common in technical interviews. They test basic iteration, string manipulation, and edge case handling.
A hash set or simple lookup structure for vowels such as {a, e, i, o, u} works best. It allows quick membership checks when verifying the first and last characters of each string.
The optimal approach is to iterate through the words between the given indices and check whether each word starts and ends with a vowel. Using a small set of vowels allows constant-time checks for both characters. This keeps the solution simple and efficient.