Skip to content

Latest commit

 

History

History
144 lines (117 loc) · 4.77 KB

File metadata and controls

144 lines (117 loc) · 4.77 KB

中文文档

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 <= 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

Solutions

Python3

class Solution:
    def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
        t = [i for i, w in enumerate(words) if w[0] in "aeiou" and w[-1] in "aeiou"]
        return [bisect_left(t, r + 1) - bisect_left(t, l) for l, r in queries]

Java

class Solution {
    public int[] vowelStrings(String[] words, int[][] queries) {
        List<Integer> t = new ArrayList<>();
        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)) {
                t.add(i);
            }
        }
        int[] ans = new int[queries.length];
        for (int i = 0; i < ans.length; ++i) {
            ans[i] = search(t, queries[i][1] + 1) - search(t, queries[i][0]);
        }
        return ans;
    }

    private int search(List<Integer> nums, int x) {
        int left = 0, right = nums.size();
        while (left < right) {
            int mid = (left + right) >> 1;
            if (nums.get(mid) >= x) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
}

C++

class Solution {
public:
    vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) {
        vector<int> t;
        unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u'};
        for (int i = 0; i < words.size(); ++i) {
            if (vowels.count(words[i][0]) && vowels.count(words[i].back())) {
                t.push_back(i);
            }
        }
        vector<int> ans;
        for (auto& q : queries) {
            int x = lower_bound(t.begin(), t.end(), q[1] + 1) - lower_bound(t.begin(), t.end(), q[0]);
            ans.push_back(x);
        }
        return ans;
    }
};

Go

func vowelStrings(words []string, queries [][]int) (ans []int) {
	vowels := "aeiou"
	t := []int{}
	for i, w := range words {
		if strings.Contains(vowels, w[:1]) && strings.Contains(vowels, w[len(w)-1:]) {
			t = append(t, i)
		}
	}
	for _, q := range queries {
		i := sort.Search(len(t), func(i int) bool { return t[i] >= q[0] })
		j := sort.Search(len(t), func(i int) bool { return t[i] >= q[1]+1 })
		ans = append(ans, j-i)
	}
	return
}

...