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 s that contains an odd number of vowels.
  • On Bob's turn, he has to remove any non-empty substring from s that 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
Vowels Game: "aebcd"aebcdvowelvowelconsonantconsonantconsonantAlice: Remove odd vowel countBob: Remove even vowel countAlice can take "a" or "e" (single vowel = odd count 1)Bob needs substrings with 0, 2, 4... vowelsResult: Alice wins = true
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)
Asked in
Google 25 Meta 20
12.0K Views
Medium Frequency
~15 min Avg. Time
450 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