给你一个下标从 0 开始的字符串数组 words 以及一个二维整数数组 queries 。
每个查询 queries[i] = [li, ri] 会要求我们统计在 words 中下标在 li 到 ri 范围内(包含 这两个值)并且以元音开头和结尾的字符串的数目。
返回一个整数数组,其中数组的第 i 个元素对应第 i 个查询的答案。
注意:元音字母是 'a'、'e'、'i'、'o' 和 'u' 。
题目链接:https://leetcode.cn/problems/count-vowel-strings-in-ranges/
# 解题思路
前缀和的典型应用:
pcnt[i]表示[0, i)区间上所有满足条件的字符串总数- 那 
pcnt[r+1] - pcnt[l]就表示区间[l,r]上的满足条件的字符串总数 
# 提交结果

# 代码
class Solution {  | |
public int[] vowelStrings(String[] words, int[][] queries) {  | |
int n = words.length;  | |
int[] pcnt = new int[n + 1];  | |
int sum = 0;  | |
for (int i = 0; i < n; ++i) {  | |
String w = words[i];  | |
pcnt[i + 1] = pcnt[i] + (check(words[i]) ? 1 : 0);  | |
        } | |
n = queries.length;  | |
int[] ans = new int[n];  | |
for (int i = 0; i < n; ++i) {  | |
ans[i] = pcnt[queries[i][1] + 1] - pcnt[queries[i][0]];  | |
        } | |
return ans;  | |
    } | |
private boolean check(String s) {  | |
char first = s.charAt(0);  | |
char last = s.charAt(s.length() - 1);  | |
return (first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u') && (last == 'a' || last == 'e' || last == 'i' || last == 'o' || last == 'u');  | |
    } | |
} |