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 i
th 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
Approach 01:
-
C++
-
Python
#include <vector> #include <string> #include <unordered_set> using namespace std; class Solution { public: vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) { unordered_set<char> vowelsSet = {'a', 'e', 'i', 'o', 'u'}; vector<int> prefixSumArray(words.size() + 1, 0); // Compute prefix sums where prefixSumArray[i] indicates the count of valid words up to index i-1 for (int index = 0; index < words.size(); ++index) { const string& word = words[index]; prefixSumArray[index + 1] = prefixSumArray[index] + (vowelsSet.count(word.front()) && vowelsSet.count(word.back()) ? 1 : 0); } // Process each query and calculate the count of valid words within the range vector<int> resultList; for (const auto& query : queries) { int queryStart = query[0]; int queryEnd = query[1]; resultList.push_back(prefixSumArray[queryEnd + 1] - prefixSumArray[queryStart]); } return resultList; } };
from typing import List class Solution: def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]: vowelsSet = {'a', 'e', 'i', 'o', 'u'} prefixSumArray = [0] * (len(words) + 1) # Compute prefix sums where prefixSumArray[i] indicates the count of valid words up to index i-1 for index, word in enumerate(words): prefixSumArray[index + 1] = prefixSumArray[index] + (1 if word[0] in vowelsSet and word[-1] in vowelsSet else 0) # Process each query and calculate the count of valid words within the range resultList = [] for queryStart, queryEnd in queries: resultList.append(prefixSumArray[queryEnd + 1] - prefixSumArray[queryStart]) return resultList
Time Complexity:
- Building Prefix Sum Array:
- We iterate through the array of words once.
- For each word, checking if the first and last characters are vowels takes \( O(1) \).
- Thus, this step has a time complexity of \( O(n) \), where \( n \) is the size of the input array words.
- Processing Queries:
- For each query, we calculate the difference of two prefix sums, which takes \( O(1) \) per query.
- With \( q \) queries, this step has a time complexity of \( O(q) \).
- Overall Time Complexity:
\( O(n + q) \), where \( n \) is the size of words and \( q \) is the number of queries.
Space Complexity:
- Prefix Sum Array:
- The prefix sum array has \( n + 1 \) elements, which requires \( O(n) \) space.
- Auxiliary Variables:
- The unordered set of vowels and additional loop variables use \( O(1) \) space.
- Overall Space Complexity:
\( O(n) \), dominated by the prefix sum array.