#2559
Medium Algorithms Count vowel strings in ranges
Array String Prefix Sum
67.8% acceptance
Feb 25, 2026
1173
72
You are given a 0-indexed array of strings words and a 2D array of integers queries.
Each query queries[i] = [li, ri] asks us to find the number of strings present at the indices ranging from li to ri (both inclusive) of words that start and end with a vowel.
Return an array ans of size queries.length, where ans[i] is the answer to the ith query.
Note that the vowel letters are 'a', 'e', 'i', 'o', and 'u'.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn vowel_strings(words: Vec<String>, queries: Vec<Vec<i32>>) -> Vec<i32> {
let vowels = |c: char| matches!(c, 'a' | 'e' | 'i' | 'o' | 'u');
let n = words.len();
let mut prefix = vec![0i32; n + 1];
for (i, w) in words.iter().enumerate() {
let chars: Vec<char> = w.chars().collect();
let ok = vowels(*chars.first().unwrap()) && vowels(*chars.last().unwrap());
prefix[i + 1] = prefix[i] + if ok { 1 } else { 0 };
}
queries
.iter()
.map(|q| prefix[q[1] as usize + 1] - prefix[q[0] as usize])
.collect()
}
}