Skip to main content
Back to problems
#3227
Medium Algorithms

Vowels game in a string

Math String Brainteaser Game Theory
77.1% acceptance
Feb 25, 2026
550
116
Alice and Bob are playing a game on a string. You are given a string s, 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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn does_alice_win(s: String) -> bool {
    // Alice wins iff the string contains at least one vowel.
    // If 0 vowels: Alice can't make a move (needs odd # vowels). Alice loses.
    // If >=1 vowels: Alice can always win with optimal play.
    s.bytes().any(|b| matches!(b, b'a' | b'e' | b'i' | b'o' | b'u'))
  }
}