2559. Count Vowel Strings in Ranges

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
private static final Set<Character> VOWEL_CHAR_SETS = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));

public int[] vowelStrings(String[] words, int[][] queries) {
int[] prefixCounts = new int[words.length + 1];
for (int i = 1; i < prefixCounts.length; i++) {
prefixCounts[i] = prefixCounts[i - 1] + (ifVowelString(words[i - 1]) ? 1 : 0);
}

int[] res = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
res[i] = prefixCounts[queries[i][1] + 1] - prefixCounts[queries[i][0]];
}
return res;
}

private boolean ifVowelString(String word) {
return VOWEL_CHAR_SETS.contains(word.charAt(0)) && VOWEL_CHAR_SETS.contains(word.charAt(word.length() - 1));
}
}

References

2559. Count Vowel Strings in Ranges