Skip to content

Commit 59078ab

Browse files
authored
Create 2559. Count Vowel Strings in Ranges (#679)
2 parents 20a6bd3 + 57d6919 commit 59078ab

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

2559. Count Vowel Strings in Ranges

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
4+
// to check if the char is vowel or not
5+
bool checkvowel(char ch){
6+
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') return true;
7+
return false;
8+
}
9+
10+
vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) {
11+
12+
vector<int>mpp(words.size()); // to store the prefix count of vowels
13+
vector<int>ans; // to store the result
14+
int cnt=0; // to count string following the constraint
15+
16+
// filling prefix vec
17+
for(int i=0;i<words.size();i++){
18+
if(words.at(i).size()==1&&checkvowel(words.at(i)[0])) cnt++;
19+
else if(checkvowel(words.at(i)[0])&&checkvowel(words.at(i)[words.at(i).size()-1]))cnt++;
20+
mpp[i]=cnt;
21+
}
22+
23+
// using prefix vec to fill the result vec
24+
for(int j=0;j<queries.size();j++){
25+
if(queries.at(j)[0]==0)ans.push_back(mpp[queries.at(j)[1]]);
26+
else ans.push_back(mpp[queries.at(j)[1]]-mpp[queries.at(j)[0]-1]);
27+
}
28+
return ans;
29+
}
30+
};

0 commit comments

Comments
 (0)