comments | difficulty | edit_url | rating | source | tags | |||
---|---|---|---|---|---|---|---|---|
true |
中等 |
1435 |
第 331 场周赛 Q2 |
|
给你一个下标从 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
我们可以预处理出所有以元音开头和结尾的字符串的下标,按顺序记录在数组
接下来,我们遍历每个查询
时间复杂度
class Solution:
def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
vowels = set("aeiou")
nums = [i for i, w in enumerate(words) if w[0] in vowels and w[-1] in vowels]
return [bisect_right(nums, r) - bisect_left(nums, l) for l, r in queries]
class Solution {
private List<Integer> nums = new ArrayList<>();
public int[] vowelStrings(String[] words, int[][] queries) {
Set<Character> vowels = Set.of('a', 'e', 'i', 'o', 'u');
for (int i = 0; i < words.length; ++i) {
char a = words[i].charAt(0), b = words[i].charAt(words[i].length() - 1);
if (vowels.contains(a) && vowels.contains(b)) {
nums.add(i);
}
}
int m = queries.length;
int[] ans = new int[m];
for (int i = 0; i < m; ++i) {
int l = queries[i][0], r = queries[i][1];
ans[i] = search(r + 1) - search(l);
}
return ans;
}
private int search(int x) {
int l = 0, r = nums.size();
while (l < r) {
int mid = (l + r) >> 1;
if (nums.get(mid) >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
}
class Solution {
public:
vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) {
unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u'};
vector<int> nums;
for (int i = 0; i < words.size(); ++i) {
char a = words[i][0], b = words[i].back();
if (vowels.count(a) && vowels.count(b)) {
nums.push_back(i);
}
}
vector<int> ans;
for (auto& q : queries) {
int l = q[0], r = q[1];
int cnt = upper_bound(nums.begin(), nums.end(), r) - lower_bound(nums.begin(), nums.end(), l);
ans.push_back(cnt);
}
return ans;
}
};
func vowelStrings(words []string, queries [][]int) []int {
vowels := map[byte]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true}
nums := []int{}
for i, w := range words {
if vowels[w[0]] && vowels[w[len(w)-1]] {
nums = append(nums, i)
}
}
ans := make([]int, len(queries))
for i, q := range queries {
l, r := q[0], q[1]
ans[i] = sort.SearchInts(nums, r+1) - sort.SearchInts(nums, l)
}
return ans
}
function vowelStrings(words: string[], queries: number[][]): number[] {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
const nums: number[] = [];
for (let i = 0; i < words.length; ++i) {
if (vowels.has(words[i][0]) && vowels.has(words[i][words[i].length - 1])) {
nums.push(i);
}
}
const search = (x: number): number => {
let l = 0,
r = nums.length;
while (l < r) {
const mid = (l + r) >> 1;
if (nums[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
return queries.map(([l, r]) => search(r + 1) - search(l));
}
我们可以创建一个长度为
接下来,我们遍历数组
最后,我们遍历每个查询
时间复杂度
class Solution:
def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
vowels = set("aeiou")
s = list(
accumulate(
(int(w[0] in vowels and w[-1] in vowels) for w in words), initial=0
)
)
return [s[r + 1] - s[l] for l, r in queries]
class Solution {
public int[] vowelStrings(String[] words, int[][] queries) {
Set<Character> vowels = Set.of('a', 'e', 'i', 'o', 'u');
int n = words.length;
int[] s = new int[n + 1];
for (int i = 0; i < n; ++i) {
char a = words[i].charAt(0), b = words[i].charAt(words[i].length() - 1);
s[i + 1] = s[i] + (vowels.contains(a) && vowels.contains(b) ? 1 : 0);
}
int m = queries.length;
int[] ans = new int[m];
for (int i = 0; i < m; ++i) {
int l = queries[i][0], r = queries[i][1];
ans[i] = s[r + 1] - s[l];
}
return ans;
}
}
class Solution {
public:
vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) {
unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u'};
int n = words.size();
int s[n + 1];
s[0] = 0;
for (int i = 0; i < n; ++i) {
char a = words[i][0], b = words[i].back();
s[i + 1] = s[i] + (vowels.count(a) && vowels.count(b));
}
vector<int> ans;
for (auto& q : queries) {
int l = q[0], r = q[1];
ans.push_back(s[r + 1] - s[l]);
}
return ans;
}
};
func vowelStrings(words []string, queries [][]int) []int {
vowels := map[byte]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true}
n := len(words)
s := make([]int, n+1)
for i, w := range words {
x := 0
if vowels[w[0]] && vowels[w[len(w)-1]] {
x = 1
}
s[i+1] = s[i] + x
}
ans := make([]int, len(queries))
for i, q := range queries {
l, r := q[0], q[1]
ans[i] = s[r+1] - s[l]
}
return ans
}
function vowelStrings(words: string[], queries: number[][]): number[] {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
const s = new Array(words.length + 1).fill(0);
words.forEach((w, i) => {
const x = +(vowels.has(w[0]) && vowels.has(w.at(-1)!));
s[i + 1] = s[i] + x;
});
return queries.map(([l, r]) => s[r + 1] - s[l]);
}
function vowelStrings(words, queries) {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
const s = new Array(words.length + 1).fill(0);
words.forEach((w, i) => {
const x = +(vowels.has(w[0]) && vowels.has(w.at(-1)));
s[i + 1] = s[i] + x;
});
return queries.map(([l, r]) => s[r + 1] - s[l]);
}