Watch 10 video solutions for Flip Game II, a medium level problem involving Math, Dynamic Programming, Backtracking. This walkthrough by CrioDo has 304,598 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem Overview: You get a string containing only '+' and '-'. Players take turns flipping any "++" into "--". The player who cannot make a move loses. The task is to determine whether the first player has a guaranteed winning strategy.
Approach 1: Brute Force Backtracking (Exponential Time)
Try every possible move by scanning the string and replacing each "++" with "--". After making a move, recursively check whether the opponent can win from that new state. If any move forces the opponent into a losing position, the current state is winning. This approach explores the full game tree and repeatedly recomputes the same states. Time complexity is O(n * 2^n) in the worst case because each position may branch into multiple new states. Space complexity is O(n) for recursion depth.
Approach 2: Backtracking with Memoization (Game DP)
Many board states appear multiple times during recursion. Store previously computed states in a hash map where the key is the current string configuration and the value indicates whether it is a winning state. During recursion, check the memo table before exploring moves. If a state is already known to be losing for the current player, the parent state becomes winning. This pruning dramatically reduces repeated exploration. Time complexity becomes roughly O(n * 2^n) in the worst case but performs much faster in practice since each state is evaluated once. Space complexity is O(2^n) for the memo table. This pattern is common in backtracking combined with memoization for combinational game states.
Approach 3: Game Theory with Segment Decomposition
Instead of storing entire strings, observe that moves only affect continuous segments of '+'. Flipping "++" splits one segment into two smaller independent segments. This allows modeling the game using Grundy numbers from impartial game theory. Compute the Grundy value for every segment length by trying all splits created by a move. If the XOR of segment Grundy values is non-zero, the position is winning. Time complexity is about O(n^2) for computing Grundy values up to length n, with O(n) space.
Recommended for interviews: The memoized backtracking approach is what most interviewers expect. Start with the brute-force recursive idea to show the game-tree reasoning, then introduce memoization to avoid recomputing states. The Grundy-number optimization demonstrates deeper understanding of combinatorial game theory but is rarely required unless the discussion goes deeper into theoretical optimization.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Backtracking | O(n * 2^n) | O(n) | Understanding the full game tree or explaining the initial idea |
| Backtracking + Memoization | O(n * 2^n) | O(2^n) | General solution used in interviews and most implementations |
| Game Theory (Grundy Numbers) | O(n^2) | O(n) | When analyzing large states using mathematical game theory |