[LeetCode] Count Vowel Strings in Ranges

2559. Count Vowel Strings in Ranges

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 in the range 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’.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) {
vector<int> psum{0};
unordered_set<char> v{'a','e','i','o','u'};
for(auto w : words) {
int now = v.count(w.front()) and v.count(w.back());
psum.push_back(psum.back() + now);
}
vector<int> res;
for(auto q : queries) {
int l = q[0], r = q[1];
res.push_back(psum[r+1] - psum[l]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/02/06/PS/LeetCode/count-vowel-strings-in-ranges/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.