leetcode daily question counts the number of vowel strings within the range

2559. Count Vowel Strings in Range

hint

medium

32

related business

You are given a zero-based  array  of strings  words and a two-dimensional array of integers  queries .

Each query  queries[i] = [li, ri] will ask us to count   the number of strings in which the middle words subscript is in  li the  ri range ( inclusive ) and start and end with a vowel.

Returns an array of integers where the th  i element of the array corresponds to  i the answer to the th query.

Note: The vowels 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 that start and end with vowels are "aba", "ece", "aa" and "e". 
The query [0,2] results in 2 (the strings "aba" and "ece"). 
The query [1,4] results in 3 (the strings "ece", "aa", "e"). 
The query [1,1] results in 0. 
Returns the result [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 this condition, so [3,2,1] is returned.

hint:

  • 1 <= words.length <= 105
  • 1 <= words[i].length <= 40
  • words[i] Consists of lowercase English letters only
  • sum(words[i].length) <= 3 * 105
  • 1 <= queries.length <= 105
  • 0 <= queries[j][0] <= queries[j][1] < words.length

Passes

10.7K

Submissions

17.5K

Passing rate

61.0%

Solution ideas: character preprocessing + prefix sum

The following is the C++ handwritten code:

class Solution {
const string p="aeiouAEIOU";
public:
    vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) {
        int n=words.size();
        vector<int> count(n,0);
        for(int i=0;i<n;i++){
            char startC=words[i][0];
            char endC=words[i][words[i].size()-1];
            if (p.find(startC) != string::npos && p.find(endC) != string::npos){
                count[i]=1;
            }
        }
        vector<int> sum(n+1,0);
        for(int i=1;i<n+1;i++)
        {
            sum[i]=sum[i-1]+count[i-1];
        }
        int m=queries.size();
        vector<int> ans(m,0);
        for(int i=0;i<m;i++){
            ans[i]=sum[queries[i][1]+1]-sum[queries[i][0]];
        }
        return ans;
    }
};

 The following is the kotlin machine generated code: (some manual modification!)

class Solution {
    fun vowelStrings(words: Array<String>, queries: Array<IntArray>): IntArray {
        val isTemp=IntArray(words.size)
        for(i in 0..words.size-1)
        {
            if(isVowelString(words[i]))
            {
                isTemp[i]=1;
            }
        }
        val counts = IntArray(queries.size)
        for (i in queries.indices) {
            val query = queries[i]
            var count = 0
            for (j in query[0]..query[1]) {
                if (isTemp[j]==1) {
                    count++
                }
            }
            counts[i] = count
        }
        return counts
    }
    fun isVowelString(word: String): Boolean {
        val vowels = setOf('a', 'e', 'i', 'o', 'u')
        return vowels.contains(word.first()) && vowels.contains(word.last())
    }
}

From the style of {}, you can see where the modification is!

Guess you like

Origin blog.csdn.net/weixin_41579872/article/details/131001612