-
Notifications
You must be signed in to change notification settings - Fork 0
/
02559-count-vowel-strings-in-ranges.cpp
43 lines (38 loc) · 1.44 KB
/
02559-count-vowel-strings-in-ranges.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
/* medium :: prefix-sum */
/*
Generate a prefix sum for each position i of size words.size(). Precompute if each
word is valid according to the problem. The number of words valid within a range
can be computed from our prefix sum by doing pf[R+1] - pf[L], +1 because our pf
array is shifted up by one.
- - - -
Time :: O(N) :: 0ms
Space :: O(N) :: 69.40MB
*/
class Solution {
public:
// Solution entrypoint :: - - - -
vector<int> vowelStrings( vector<string>& words, vector<vector<int>>& queries )
{
const unordered_set<char> vowels = { 'a', 'e', 'i', 'o', 'u' };
int32_t n = words.size();
vector<int32_t> pf( n + 1, 0 );
for ( int32_t i = 0; i < n; ++i )
{
// Push forward current prefix :
pf[i + 1] = pf[i];
// Qualified Word :
if ( vowels.contains( words[i][0] ) && vowels.contains( words[i].back() ) ) {
pf[i + 1]++;
}
}
n = queries.size();
vector<int32_t> ret( n, 0 );
for ( int32_t i = 0; i < n; ++i )
{
// Right prefix - Left prefix :
ret[i] = pf[queries[i][1] + 1] - pf[queries[i][0]];
}
return ret;
}
};
// End. :: - - - -