Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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:
s that contains an odd number of vowels.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.
Example 1:
Input: s = "leetcoder"
Output: true
Explanation:
Alice can win the game as follows:
s = "leetcoder" which contains 3 vowels. The resulting string is s = "der".s = "der" which contains 0 vowels. The resulting string is s = "er".s = "er" which contains 1 vowel.Example 2:
Input: s = "bbcd"
Output: false
Explanation:
There is no valid play for Alice in her first turn, so Alice loses the game.
Constraints:
1 <= s.length <= 105s consists only of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"leetcoder"
"bbcd"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3227: Vowels Game in a String
class Solution {
public boolean doesAliceWin(String s) {
for (int i = 0; i < s.length(); ++i) {
if ("aeiou".indexOf(s.charAt(i)) != -1) {
return true;
}
}
return false;
}
}
// Accepted solution for LeetCode #3227: Vowels Game in a String
func doesAliceWin(s string) bool {
vowels := "aeiou"
for _, c := range s {
if strings.ContainsRune(vowels, c) {
return true
}
}
return false
}
# Accepted solution for LeetCode #3227: Vowels Game in a String
class Solution:
def doesAliceWin(self, s: str) -> bool:
vowels = set("aeiou")
return any(c in vowels for c in s)
// Accepted solution for LeetCode #3227: Vowels Game in a String
impl Solution {
pub fn does_alice_win(s: String) -> bool {
let vowels = "aeiou";
for c in s.chars() {
if vowels.contains(c) {
return true;
}
}
false
}
}
// Accepted solution for LeetCode #3227: Vowels Game in a String
function doesAliceWin(s: string): boolean {
const vowels = 'aeiou';
for (const c of s) {
if (vowels.includes(c)) {
return true;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.