-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2559. Count Vowel Strings in Ranges
- Loading branch information
1 parent
20a6bd3
commit 57d6919
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution { | ||
public: | ||
|
||
// to check if the char is vowel or not | ||
bool checkvowel(char ch){ | ||
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') return true; | ||
return false; | ||
} | ||
|
||
vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) { | ||
|
||
vector<int>mpp(words.size()); // to store the prefix count of vowels | ||
vector<int>ans; // to store the result | ||
int cnt=0; // to count string following the constraint | ||
|
||
// filling prefix vec | ||
for(int i=0;i<words.size();i++){ | ||
if(words.at(i).size()==1&&checkvowel(words.at(i)[0])) cnt++; | ||
else if(checkvowel(words.at(i)[0])&&checkvowel(words.at(i)[words.at(i).size()-1]))cnt++; | ||
mpp[i]=cnt; | ||
} | ||
|
||
// using prefix vec to fill the result vec | ||
for(int j=0;j<queries.size();j++){ | ||
if(queries.at(j)[0]==0)ans.push_back(mpp[queries.at(j)[1]]); | ||
else ans.push_back(mpp[queries.at(j)[1]]-mpp[queries.at(j)[0]-1]); | ||
} | ||
return ans; | ||
} | ||
}; |