-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCountVowelsStringInRanges.java
More file actions
32 lines (31 loc) · 954 Bytes
/
CountVowelsStringInRanges.java
File metadata and controls
32 lines (31 loc) · 954 Bytes
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
class Solution {
public int[] vowelStrings(String[] words, int[][] queries) {
int n = words.length;
int prefixSum[] = new int[n];
prefixSum[0] = isVowel(words[0]);
for(int i=1;i<n;i++){
prefixSum[i] = prefixSum[i-1] + isVowel(words[i]);
}
int m = queries.length;
int ans[] = new int[m];
for(int i=0;i<m;i++){
int l = queries[i][0];
int r = queries[i][1];
int res = prefixSum[r];
if(l!=0){
res-= prefixSum[l-1];
}
ans[i] = res;
}
return ans;
}
public int isVowel(String word){
HashSet<Character> set = new HashSet<>(Arrays.asList('a','e','i','o','u'));
char first = word.charAt(0);
char last = word.charAt(word.length()-1);
if(set.contains(first) && set.contains(last)){
return 1;
}
return 0;
}
}