Vowels Game in a String
MediumUpdated: Sep 12, 2025
Practice on:
Problem
Alice and Bob are playing a game on a string.
You are given a string s, Alice and Bob will take turns playing the following game where Alice starts first :
- On Alice's turn, she has to remove any non-empty substring from
sthat contains an odd number of vowels. - On Bob's turn, he has to remove any non-empty substring from
sthat contains an even number of vowels.
The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play optimally.
Return true if Alice wins the game, and false otherwise.
The English vowels are: a, e, i, o, and u.
Examples
Example 1
Input: s = "leetcoder"
Output: true
Explanation:
Alice can win the game as follows:
* Alice plays first, she can delete the underlined substring in `s = "_**leetco**_ der"` which contains 3 vowels. The resulting string is `s = "der"`.
* Bob plays second, he can delete the underlined substring in `s = "_**d**_ er"` which contains 0 vowels. The resulting string is `s = "er"`.
* Alice plays third, she can delete the whole string `s = "**_er_** "` which contains 1 vowel.
* Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game.
Example 2
Input: s = "bbcd"
Output: false
Explanation:
There is no valid play for Alice in her first turn, so Alice loses the game.
Constraints
1 <= s.length <= 10^5sconsists only of lowercase English letters.
Solution
Method 1 – Parity and Game Theory
Intuition
Alice wins if there is at least one vowel in the string, because she can always remove a substring containing a vowel on her first turn. If there are no vowels, Alice cannot make a move and loses.
Approach
- Iterate through the string.
- If you find any vowel (
a,e,i,o, oru), Alice can win immediately. - If no vowels are found, Alice loses.
Code
C++
class Solution {
public:
bool doesAliceWin(string s) {
for (char ch : s) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
return true;
}
}
return false;
}
};
Go
func doesAliceWin(s string) bool {
for _, ch := range s {
if ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' {
return true
}
}
return false
}
Java
class Solution {
public boolean doesAliceWin(String s) {
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
return true;
}
}
return false;
}
}
Kotlin
class Solution {
fun doesAliceWin(s: String): Boolean {
for (ch in s) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
return true
}
}
return false
}
}
Python
class Solution:
def doesAliceWin(self, s: str) -> bool:
for ch in s:
if ch in 'aeiou':
return True
return False
Rust
impl Solution {
pub fn does_alice_win(s: &str) -> bool {
for ch in s.chars() {
if matches!(ch, 'a' | 'e' | 'i' | 'o' | 'u') {
return true;
}
}
false
}
}
TypeScript
class Solution {
doesAliceWin(s: string): boolean {
for (const ch of s) {
if ('aeiou'.includes(ch)) {
return true;
}
}
return false;
}
}
Complexity
- ⏰ Time complexity:
O(n)— One pass to count vowels. - 🧺 Space complexity:
O(1)— Only a few variables are used.