Vowels Game in a String - Problem
Vowels Game in a String is a fascinating strategic game theory problem where two players compete optimally to avoid losing.
๐ฎ The Game Rules:
โข Alice starts first and must remove any non-empty substring containing an odd number of vowels (a, e, i, o, u)
โข Bob follows and must remove any non-empty substring containing an even number of vowels
โข Players alternate turns until one cannot make a valid move
โข The first player unable to move loses the game
๐ง Your Task: Given a string
Key Insight: This isn't about simulating the entire game - it's about finding the mathematical pattern that determines the winner based on the initial string structure.
๐ฎ The Game Rules:
โข Alice starts first and must remove any non-empty substring containing an odd number of vowels (a, e, i, o, u)
โข Bob follows and must remove any non-empty substring containing an even number of vowels
โข Players alternate turns until one cannot make a valid move
โข The first player unable to move loses the game
๐ง Your Task: Given a string
s, determine if Alice wins when both players play optimally. Return true if Alice wins, false if Bob wins.Key Insight: This isn't about simulating the entire game - it's about finding the mathematical pattern that determines the winner based on the initial string structure.
Input & Output
example_1.py โ Basic case with vowels
$
Input:
s = "leetcode"
โบ
Output:
true
๐ก Note:
Alice wins because there are vowels in the string. She can make moves like removing "e" (1 vowel, odd) or "ee" (2 vowels, even - wait, this is Bob's move). Alice can remove "e" (1 vowel - odd), then Bob must remove a substring with even vowels, and so on. Since Alice goes first and vowels exist, she can always find a winning strategy.
example_2.py โ String with no vowels
$
Input:
s = "bbcd"
โบ
Output:
false
๐ก Note:
Alice loses because there are no vowels in the string. She cannot make any valid move (needs odd vowel count โฅ 1), so she loses immediately on her first turn.
example_3.py โ Single vowel
$
Input:
s = "a"
โบ
Output:
true
๐ก Note:
Alice wins by removing the entire string "a" which contains 1 vowel (odd number). After this move, Bob faces an empty string and cannot make any move, so Alice wins.
Visualization
Tap to expand
Understanding the Visualization
1
Analyze Game Rules
Alice removes odd-vowel substrings, Bob removes even-vowel substrings
2
Find Winning Pattern
If vowels exist, Alice can remove entire string (odd count โฅ 1)
3
Apply Strategy
Alice's optimal first move: take the whole string when possible
4
Determine Winner
Count vowels: >0 means Alice wins, =0 means Alice loses
Key Takeaway
๐ฏ Key Insight: This game theory problem has a surprisingly simple solution - Alice wins if and only if there's at least one vowel in the string, because she can always remove the entire string as her winning move!
Time & Space Complexity
Time Complexity
O(n)
Single pass through string to count vowels
โ Linear Growth
Space Complexity
O(1)
Only storing vowel count variable
โ Linear Space
Constraints
- 1 โค s.length โค 105
- s consists only of lowercase English letters
- The game assumes both players play optimally
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code