Count the Number of Vowel Strings in Range - 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'.

Return the number of vowel strings words[i] where i belongs to the inclusive range [left, right].

Input & Output

Example 1 — Basic Range
$ Input: words = ["are","amy","u"], left = 0, right = 2
Output: 2
💡 Note: "are" starts with 'a' and ends with 'e' (both vowels). "u" starts and ends with 'u' (vowel). "amy" starts with 'a' but ends with 'y' (not a vowel).
Example 2 — Partial Range
$ Input: words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
Output: 3
💡 Note: In range [1,4]: "aeo" (a-o), "ooo" (o-o), "artro" (a-o) are vowel strings. "mu" (m-u) is not because it starts with 'm'.
Example 3 — Single Element
$ Input: words = ["a","b","c"], left = 1, right = 1
Output: 0
💡 Note: Only checking words[1] = "b", which starts and ends with 'b' (not a vowel).

Constraints

  • 1 ≤ words.length ≤ 1000
  • 1 ≤ words[i].length ≤ 10
  • words[i] consists of only lowercase English letters
  • 0 ≤ left ≤ right < words.length

Visualization

Tap to expand
Count Vowel Strings in Range INPUT words[] array: i=0 i=1 i=2 "are" "amy" "u" Range [left, right] left = 0 right = 2 vowels: a, e, i, o, u Check each word in range starts AND ends with vowel ALGORITHM STEPS 1 Initialize counter count = 0 2 Loop i from left to right for i in [0, 1, 2] 3 Check each word first char in vowels? last char in vowels? 4 Increment if valid if both true: count++ Word Analysis: "are": a...e OK "amy": a...y NO "u": u...u OK count = 2 FINAL RESULT Vowel Strings Found: "are" starts: a, ends: e "u" starts: u, ends: u "amy" ends with y (not vowel) Output: 2 2 vowel strings found Key Insight: A vowel string must both START and END with a vowel (a, e, i, o, u). We iterate only through the specified range [left, right] and check each word's first and last character. Time: O(n), Space: O(1) TutorialsPoint - Count the Number of Vowel Strings in Range | Optimal Solution
Asked in
Amazon 25 Microsoft 18
23.8K Views
Medium Frequency
~5 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen