Vowels Game in a String (LeetCode 3227): Solution and Explanation
Alice removes substrings with an odd number of vowels, Bob with an even number. Alice wins exactly when the string contains at least one vowel, so the solution is a single scan for a vowel.
Intuition
Simulating the game is hopeless, so look for who has a move at all. With no vowels, every substring has 0 vowels, which is even, so Alice has no legal first move and loses immediately.
With an odd number of vowels, Alice removes the entire string in one move and Bob faces an empty board.
With an even, positive number of vowels, Alice removes everything before the last vowel. That piece has an odd count, and exactly one vowel remains. Bob can only remove pieces with an even vowel count, which here means vowel-free pieces, so the vowel always survives. On her next turn Alice removes everything that is left.
Approach: Check for any vowel
- Scan the characters of
sfrom left to right. - As soon as one is in "aeiou", return
true: Alice has a winning strategy. - If the scan ends with no vowel found, return
false: Alice cannot make a first move.
Step-by-step example
s = "leetcoder"
- s[0] = 'l' is a consonant, keep scanning.
- s[1] = 'e' is a vowel, so return true.
- Why it holds: the string has 4 vowels (even). Alice removes "leetco" with 3 vowels, leaving "der" with one vowel.
- Bob can only take vowel-free pieces such as "d" or "r". The 'e' survives.
- Alice removes everything left, which has 1 vowel, and Bob has no move.
Code: Python, Java, C++
1class Solution:
2 def doesAliceWin(self, s: str) -> bool:
3 for c in s:
4 if c in "aeiou":
5 return True
6 return False1class Solution {
2 public boolean doesAliceWin(String s) {
3 for (char c : s.toCharArray()) {
4 if ("aeiou".indexOf(c) >= 0) {
5 return true;
6 }
7 }
8 return false;
9 }
10}1class Solution {
2public:
3 bool doesAliceWin(string s) {
4 for (char c : s) {
5 if (string("aeiou").find(c) != string::npos) {
6 return true;
7 }
8 }
9 return false;
10 }
11};Time and space complexity
O(n)
At most one pass over the string, and it stops at the first vowel.
O(1)
No extra storage beyond the loop variable.
Other approaches
Count vowels (time O(n), space O(1))
Count all vowels and return count > 0. Same answer, but it always scans the whole string.
Game search (time Exponential, space Exponential)
Explore all removals with memoization over remaining strings. Correct in principle but exponential, and only useful for discovering the pattern on tiny inputs.
Common mistakes
- Thinking the parity of the vowel count decides the winner. Both odd and even positive counts are wins for Alice.
- Forgetting that zero is even, so Bob can always remove a consonant-only piece when one exists. That does not help him, because the last vowel stays.
- Treating 'y' as a vowel. Only a, e, i, o and u count.
Frequently asked questions
Why can Bob never remove the last vowel?
Any substring containing it has exactly one vowel, which is odd. Bob may only remove substrings with an even number of vowels, so every piece he takes is vowel-free.
Does it matter where the vowels are?
No. Alice's two strategies, take everything or take everything before the last vowel, work for any arrangement.
Can Bob ever win?
Only when s has no vowels. Then Alice cannot make her first move.