当前位置: 首页> 财经> 金融 > b2b2c平台商城系统_网站建设哪家售后做的好_seo深圳培训班_免费刷粉网站推广免费

b2b2c平台商城系统_网站建设哪家售后做的好_seo深圳培训班_免费刷粉网站推广免费

时间:2025/7/12 5:01:15来源:https://blog.csdn.net/sinat_41679123/article/details/144875508 浏览次数:0次
b2b2c平台商城系统_网站建设哪家售后做的好_seo深圳培训班_免费刷粉网站推广免费

Description

You are given a 0-indexed array of strings words and a 2D array of integers queries.

Each query queries[i] = [li, ri] asks us to find the number of strings present in the range li to ri (both inclusive) of words that start and end with a vowel.

Return an array ans of size queries.length, where ans[i] is the answer to the ith query.

Note that the vowel letters are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.

Example 1:

Input: words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]
Output: [2,3,0]
Explanation: The strings starting and ending with a vowel are "aba", "ece", "aa" and "e".
The answer to the query [0,2] is 2 (strings "aba" and "ece").
to query [1,4] is 3 (strings "ece", "aa", "e").
to query [1,1] is 0.
We return [2,3,0].

Example 2:

Input: words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]]
Output: [3,2,1]
Explanation: Every string satisfies the conditions, so we return [3,2,1].

Constraints:

1 <= words.length <= 10^5
1 <= words[i].length <= 40
words[i] consists only of lowercase English letters.
sum(words[i].length) <= 3 * 10^5
1 <= queries.length <= 10^5
0 <= li <= ri < words.length

Solution

Typical prefix sum. Use a prefix sum hashmap to store the count of all the vowels before current position, then for the query, we just need to prefix[query_end] - prefix[query_start]. Note because the query is inclusive at both sides, we need to twist a little bit.

Time complexity: o ( q u e r y . l e n + w o r d s . l e n ) o(query.len + words.len) o(query.len+words.len)
Space complexity: o ( w o r d s . l e n ) o(words.len) o(words.len)

Code

class Solution:def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:prefix_sum = {-1: 0}for i in range(len(words)):if words[i][0] in ('a', 'e', 'i', 'o', 'u') and words[i][-1] in ('a', 'e', 'i', 'o', 'u'):prefix_sum[i] = prefix_sum[i - 1] + 1else:prefix_sum[i] = prefix_sum[i - 1]res = []for q_start, q_end in queries:q_start -= 1res.append(prefix_sum[q_end] - prefix_sum[q_start])return res
关键字:b2b2c平台商城系统_网站建设哪家售后做的好_seo深圳培训班_免费刷粉网站推广免费

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: