2691. Count Vowel Strings In Ranges¶
Difficulty: Medium
LeetCode Problem View on GitHub
2691. Count Vowel Strings in Ranges
Medium
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 <= 1051 <= words[i].length <= 40words[i]consists only of lowercase English letters.sum(words[i].length) <= 3 * 1051 <= queries.length <= 1050 <= li <= ri < words.length
Solution¶
class Solution {
public int[] vowelStrings(String[] words, int[][] queries) {
int n = words.length;
int arr[] = new int[n];
for(int i = 0; i < n; i++) {
String current = words[i];
if(isvowel(current.charAt(0)) && isvowel(current.charAt(current.length() - 1))) arr[i] = 1;
else arr[i] = 0;
}
int sum = 0;
int pre[] = new int[n];
for(int i = 0; i < n; i++) {
sum += arr[i];
pre[i] = sum;
}
int res[] = new int[queries.length];
int k = 0;
for(int current[] : queries) {
int l = current[0], r = current[1];
if(l == 0) res[k++] = pre[r];
else res[k++] = pre[r] - pre[l - 1];
}
return res;
}
private boolean isvowel(char current) {
if ((current == 'a') || (current == 'e') || (current == 'i' ) || (current == 'o') || (current == 'u')) return true;
return false;
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here