-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
46 lines (39 loc) · 1.27 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bitset>
#include <cstddef>
#include <cstdlib>
#include <string>
#include <vector>
constexpr std::bitset<26> isVowel = []() -> std::bitset<26> {
std::bitset<26> vowel{};
for (char v : {'a', 'e', 'i', 'o', 'u'}) {
vowel.set(v - 'a');
}
return vowel;
}();
class Solution {
public:
std::vector<int> vowelStrings(std::vector<std::string>& words,
std::vector<std::vector<int>>& queries) {
// query[i] = [l, r], to find the number of strings in words[l..r] inclusive
// that start and end with a vowel {'a', 'e', 'i', 'o', 'u'}
// Hm, just do a prefix preprocessing of the number of words that start with
// a vowel.
// Start and End with vowel
auto pred = [](std::string_view word) -> int {
return isVowel.test(word[0] - 'a') && isVowel.test(word.back() - 'a');
};
std::vector<int> prefix(words.size() + 1, 0);
prefix[0] = pred(words[0]);
for (int i = 1; i < words.size() + 1; ++i) {
prefix[i] = prefix[i - 1] + pred(words[i - 1]);
}
std::vector<int> answer;
answer.reserve(queries.size());
for (const auto& query : queries) {
int l = query[0];
int r = query[1]; // inclusive end
answer.push_back(prefix[r + 1] - prefix[l]);
}
return answer;
}
};