Vowels Game in a String - Problem
Alice and Bob are playing a game on a string. You are given a string s, and 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.
Input & Output
Example 1 — String with vowels
$
Input:
s = "leetcodeisgreat"
›
Output:
true
💡 Note:
Alice can remove any single vowel (like 'e') which has odd count 1, forcing the game to continue until she wins
Example 2 — String with no vowels
$
Input:
s = "bbcd"
›
Output:
false
💡 Note:
No vowels in string, so Alice cannot make any move and loses immediately
Example 3 — Single vowel
$
Input:
s = "a"
›
Output:
true
💡 Note:
Alice takes the single vowel 'a' (odd count = 1), Bob has no moves left, Alice wins
Constraints
- 1 ≤ s.length ≤ 105
- s consists only of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
String with mix of vowels and consonants
2
Game Rules
Alice takes odd vowel count substrings, Bob takes even
3
Winner
Alice wins if any vowel exists in the string
Key Takeaway
🎯 Key Insight: Alice always wins if the string contains any vowel, because she can take individual vowels (odd count = 1)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code