class Solution {
public:
vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) {
}
};
6347. 统计范围内的元音字符串数
给你一个下标从 0 开始的字符串数组 words
以及一个二维整数数组 queries
。
每个查询 queries[i] = [li, ri]
会要求我们统计在 words
中下标在 li
到 ri
范围内(包含 这两个值)并且以元音开头和结尾的字符串的数目。
返回一个整数数组,其中数组的第 i
个元素对应第 i
个查询的答案。
注意:元音字母是 'a'
、'e'
、'i'
、'o'
和 'u'
。
示例 1:
输入:words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]] 输出:[2,3,0] 解释:以元音开头和结尾的字符串是 "aba"、"ece"、"aa" 和 "e" 。 查询 [0,2] 结果为 2(字符串 "aba" 和 "ece")。 查询 [1,4] 结果为 3(字符串 "ece"、"aa"、"e")。 查询 [1,1] 结果为 0 。 返回结果 [2,3,0] 。
示例 2:
输入:words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]] 输出:[3,2,1] 解释:每个字符串都满足这一条件,所以返回 [3,2,1] 。
提示:
1 <= words.length <= 105
1 <= words[i].length <= 40
words[i]
仅由小写英文字母组成sum(words[i].length) <= 3 * 105
1 <= queries.length <= 105
0 <= queries[j][0] <= queries[j][1] < words.length
原站题解
javascript 解法, 执行用时: 140 ms, 内存消耗: 67.8 MB, 提交时间: 2023-11-07 07:23:19
/** * @param {string[]} words * @param {number[][]} queries * @return {number[]} */ var vowelStrings = function(words, queries) { let n = words.length; let prefixSums = new Array(n + 1).fill(0); for (let i = 0; i < n; i++) { let value = isVowelString(words[i]) ? 1 : 0; prefixSums[i + 1] = prefixSums[i] + value; } let ans = []; for (let i = 0; i < queries.length; i++) { let start = queries[i][0], end = queries[i][1]; ans.push(prefixSums[end + 1] - prefixSums[start]); } return ans; } function isVowelString(word) { return isVowelLetter(word[0]) && isVowelLetter(word[word.length - 1]); } function isVowelLetter(c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; }
java 解法, 执行用时: 3 ms, 内存消耗: 81.9 MB, 提交时间: 2023-11-07 07:23:00
class Solution { public int[] vowelStrings(String[] words, int[][] queries) { int n = words.length; int[] prefixSums = new int[n + 1]; for (int i = 0; i < n; i++) { int value = isVowelString(words[i]) ? 1 : 0; prefixSums[i + 1] = prefixSums[i] + value; } int q = queries.length; int[] ans = new int[q]; for (int i = 0; i < q; i++) { int start = queries[i][0], end = queries[i][1]; ans[i] = prefixSums[end + 1] - prefixSums[start]; } return ans; } public boolean isVowelString(String word) { return isVowelLetter(word.charAt(0)) && isVowelLetter(word.charAt(word.length() - 1)); } public boolean isVowelLetter(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } }
cpp 解法, 执行用时: 132 ms, 内存消耗: 62.3 MB, 提交时间: 2023-02-06 10:11:44
class Solution { public: vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) { int n = queries.size(); vector<int> ans(n); int m = words.size(); vector<int> flag(m); for (int i = 0; i < m; ++i) { string str = words[i]; int len = str.size(); if ((str[0] == 'a' || str[0] == 'e' || str[0] == 'i' || str[0] == 'o' || str[0] == 'u') && (str[len-1] == 'a' || str[len-1] == 'e' || str[len-1] == 'i' || str[len-1] == 'o' || str[len-1] == 'u')) flag[i] = 1; else flag[i] = 0; } vector<int> pre(m+1); for (int i = 1; i <= m; ++i) { pre[i] = pre[i-1] + flag[i-1]; } for (int i = 0; i < n; ++i) { int num = 0; ans[i] = pre[queries[i][1] + 1] - pre[queries[i][0]]; } return ans; } };
golang 解法, 执行用时: 112 ms, 内存消耗: 19.1 MB, 提交时间: 2023-02-06 10:10:26
func vowelStrings(words []string, queries [][]int) []int { sum := make([]int, len(words)+1) for i, w := range words { sum[i+1] = sum[i] if strings.Contains("aeiou", w[:1]) && strings.Contains("aeiou", w[len(w)-1:]) { sum[i+1]++ } } ans := make([]int, len(queries)) for i, q := range queries { ans[i] = sum[q[1]+1] - sum[q[0]] } return ans }
python3 解法, 执行用时: 80 ms, 内存消耗: 45.9 MB, 提交时间: 2023-02-06 10:09:53
''' 前缀和 ''' class Solution: def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]: s = list(accumulate((w[0] in "aeiou" and w[-1] in "aeiou" for w in words), initial=0)) return [s[r + 1] - s[l] for l, r in queries]