Vowels Game in a String

medium math game theory brainteaser

Problem

Alice and Bob alternate turns on a string s, with Alice going first. On her turn Alice removes any non-empty substring containing an odd number of vowels; on his turn Bob removes any non-empty substring containing an even number of vowels (zero counts as even). The first player who cannot move loses. With both playing optimally, return true if Alice wins. Vowels are a, e, i, o, u.

Inputs = "leetcoder"
Outputtrue
The string has vowels (e, e, o, e), so Alice can win — e.g. she removes "leetco" (3 vowels), and from there she always has a reply to Bob.

def doesAliceWin(s):
    vowels = set("aeiou")          # the five English vowels
    # Game theory result: with optimal play Alice loses
    # only when the string has NO vowels at all. Otherwise
    # she always wins, so we just look for one vowel.
    for ch in s:
        if ch in vowels:
            return True            # a vowel exists -> Alice wins
    return False                   # no vowels -> Bob wins
function doesAliceWin(s) {
  const vowels = new Set(["a", "e", "i", "o", "u"]);
  // Game theory result: with optimal play Alice loses
  // only when the string has NO vowels at all. Otherwise
  // she always wins, so we just look for one vowel.
  for (const ch of s) {
    if (vowels.has(ch)) {
      return true;               // a vowel exists -> Alice wins
    }
  }
  return false;                  // no vowels -> Bob wins
}
boolean doesAliceWin(String s) {
    String vowels = "aeiou";       // the five English vowels
    // Game theory result: with optimal play Alice loses
    // only when the string has NO vowels at all. Otherwise
    // she always wins, so we just look for one vowel.
    for (int i = 0; i < s.length(); i++) {
        if (vowels.indexOf(s.charAt(i)) >= 0) {
            return true;           // a vowel exists -> Alice wins
        }
    }
    return false;                  // no vowels -> Bob wins
}
bool doesAliceWin(string s) {
    string vowels = "aeiou";       // the five English vowels
    // Game theory result: with optimal play Alice loses
    // only when the string has NO vowels at all. Otherwise
    // she always wins, so we just look for one vowel.
    for (char ch : s) {
        if (vowels.find(ch) != string::npos) {
            return true;           // a vowel exists -> Alice wins
        }
    }
    return false;                  // no vowels -> Bob wins
}
Time: O(n) Space: O(1)