You are given:
words
: These are lowercase English words.queries
: Each query is a range [li, ri]
.Task:
For each query [li, ri]
, count how many words in the range (from index li
to ri
, inclusive) start and end with a vowel.
The vowels are 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
.
Return an array ans
where:
ans[i]
is the answer to the i-th
query.Input: words = ["aba", "bcb", "ece", "aa", "e"],
queries = [[0, 2], [1, 4], [1, 1]]
Output: [2, 3, 0]
Words starting and ending with vowels:
"aba"
, "ece"
, "aa"
, "e"
.
[0, 2]
:"aba"
and "ece"
, so the result is 2
.[1, 4]
:"ece"
, "aa"
, "e"
, so the result is 3
.[1, 1]
:0
.Input: words = ["a","e","i"],
queries = [[0,2],[0,1],[2,2]]
Output: [2, 3, 0]
All words start and end with vowels.
[0, 2]
:"a"
, "e"
, "i"
) satisfy the condition, so the result is 3
.[0, 1]
:"a"
and "e"
, so the result is 2
.[2, 2]
:"i"
, so the result is 1
.words.length
): Up to .words[i].length
): Up to .queries.length
): Up to .Loading component...
Loading component...
Main Function is not defined.
INPUT: words = ["aba", "bcb", "ece", "aa", "e"], queries = [[0, 2], [1, 4], [1, 1]]
OUTPUT : [2, 3, 0]
public static int[] vowelString( string[] words, int[][] queries){
int n = words.length;
int[] Prefix = new int[n + 1];
Set<Character> vowels = new HashSet<>(Arrays.asList('a' , 'e' , 'i' , 'o' , 'u'));
for (int i = 0; i < n ; i ++){
Prefix[ i+1 ] = Prefix[ i ];
if(vowels.contains(words[i].charAt(0)) && vowels.contains(words[i].charAt(words[i].length()-1))){
Prefix[i+1]++;
}//If End
}//Loop End
int ANS = new int[queries.length];
for(int i = 0; i < queries.length; i++){
ANS[i] = Prefix[queries[i][1] + 1] - Prefix[queries[i][0]];
}//Loop End
return ANS;
}//function end
Utility Function is not required.