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 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
Vowels Game StrategyAlice's Rulesโ€ข Goes firstโ€ข Removes substrings with ODD number of vowelsBob's Rulesโ€ข Goes secondโ€ข Removes substrings with EVEN number of vowelsWinning StrategyIf string has vowels: Alice removes ENTIRE stringโ€ข Entire string has โ‰ฅ1 vowels (odd count)โ€ข Bob left with empty string โ†’ no moves โ†’ Alice wins!If no vowels: Alice has no valid moves โ†’ Alice losesโœ“Alice winsvowels > 0โœ—Alice losesvowels = 0
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing vowel count variable

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists only of lowercase English letters
  • The game assumes both players play optimally
Asked in
Google 25 Microsoft 18 Amazon 15 Meta 12
24.7K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen