3227: Vowels-Game-in-a-String
Medium
table of contents
For this question, realise that a string can have:
Therefore, we can just define a function to return if a character is a vowel:
bool isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
Finally, we can iterate through the string and return a Bob win if and only if there are zero vowels in the string. Otherwise, we can return an Alice win.
bool doesAliceWin(string s) {
int count = 0;
for (char& c : s) {
if(isVowel(c)) {
++count;
}
}
return count == 0 ? false : true;
}
code
class Solution {
public:
bool doesAliceWin(string s) {
int count = 0;
for (char& c : s) {
if(isVowel(c)) {
++count;
}
}
return count == 0 ? false : true;
}
private:
bool isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
};