[LeetCode] 2559. Count Vowel Strings in Ranges

Posted CNoodle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 2559. Count Vowel Strings in Ranges相关的知识,希望对你有一定的参考价值。

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 <= 105
  • 1 <= words[i].length <= 40
  • words[i] consists only of lowercase English letters.
  • sum(words[i].length) <= 3 * 105
  • 1 <= queries.length <= 105
  • 0 <= li <= ri < words.length

统计范围内的元音字符串数。

给你一个下标从 0 开始的字符串数组 words 以及一个二维整数数组 queries 。

每个查询 queries[i] = [li, ri] 会要求我们统计在 words 中下标在 li 到 ri 范围内(包含 这两个值)并且以元音开头和结尾的字符串的数目。

返回一个整数数组,其中数组的第 i 个元素对应第 i 个查询的答案。

注意:元音字母是 \'a\'、\'e\'、\'i\'、\'o\' 和 \'u\' 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-vowel-strings-in-ranges
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是前缀和。首先我们需要一个与 words 数组等长的数组 goods 记录哪些单词满足以元音开头和结尾这个条件,遍历 words 数组的时候,判断每一个单词 words[i] 是否满足条件,如果是,则在 goods[i] 处标记为 1。我们还需要另外一个数组 presums 记录 goods 数组的前缀和。最后我们通过 presums 快速地找到每一个 query 的答案。

时间O(n)

空间O(n)

Java实现

 class Solution 
     public int[] vowelStrings(String[] words, int[][] queries) 
         int[] good = new int[words.length];
         for (int i = 0; i < words.length; i++) 
             if (helper(words[i])) 
                 good[i] = 1;
             
         
 
         int[] presum = new int[words.length + 1];
         presum[0] = good[0];
         for (int i = 0; i < words.length; i++) 
             presum[i + 1] = presum[i] + good[i];
         
 
         int n = queries.length;
         int[] res = new int[n];
         for (int i = 0; i < n; i++) 
             int start = queries[i][0];
             int end = queries[i][1];
             res[i] = presum[end + 1] - presum[start];
         
         return res;
     
 
     private boolean helper(String word) 
         Set<Character> set = new HashSet<>();
         set.add(\'a\');
         set.add(\'e\');
         set.add(\'i\');
         set.add(\'o\');
         set.add(\'u\');
         if (set.contains(word.charAt(0)) && set.contains(word.charAt(word.length() - 1))) 
             return true;
         
         return false;
     
 

 

LeetCode 题目总结

分页携带参数问题

之前写过一个demo点击下一页查询条件就没了

后来找到方法是要将参数一并写入url中

jsp代码如下:

<a href="?start=$vo.start+vo.count&custName=$vo.custName&custSource=$vo.custSource&custIndustry=$vo.custIndustry&custLevel=$vo.custLevel">下一页</a>

即:

http://localhost:8080/ssm_curd_/customer/list.do?start=20&custName=马&custSource=6&custIndustry=2&custLevel=22
第一个问好代表第一个参数,后面的参数用&连接

以上是关于[LeetCode] 2559. Count Vowel Strings in Ranges的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode1147 段式回文

分页携带参数问题

poj 2559(栈的应用)

单调栈(POJ2559)

POJ 2559 单调栈

POJ2559 单调栈