-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.java
27 lines (24 loc) · 850 Bytes
/
solution.java
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
import java.util.*;
class Solution {
public int[] vowelStrings(String[] words, int[][] queries) {
Set<Character> vowels = Set.of('a', 'e', 'i', 'o', 'u');
int n = words.length;
int[] prefix = new int[n];
// Precompute the prefix sum
for (int i = 0; i < n; i++) {
if (vowels.contains(words[i].charAt(0)) && vowels.contains(words[i].charAt(words[i].length() - 1))) {
prefix[i] = 1;
}
if (i > 0) {
prefix[i] += prefix[i - 1];
}
}
// Answer the queries
int[] result = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int l = queries[i][0], r = queries[i][1];
result[i] = prefix[r] - (l > 0 ? prefix[l - 1] : 0);
}
return result;
}
}