Welcome to the Flip Game II - a strategic two-player game that tests your ability to think ahead!

You and your friend are playing with a string currentState containing only '+' and '-' characters. On each turn, a player must flip two consecutive "++" into "--". The game continues until no more moves are possible.

The Challenge: Determine if the starting player (you) can guarantee a win with optimal play from both sides.

Rules:

  • Players take turns flipping consecutive "++" to "--"
  • The player who cannot make a move loses
  • Both players play optimally

Goal: Return true if you (the first player) can guarantee a win, false otherwise.

Example: For "+++", you can flip the first two to get "-++", then your opponent has no moves left - you win!

Input & Output

example_1.py โ€” Basic Winning Case
$ Input: currentState = "++++"
โ€บ Output: true
๐Ÿ’ก Note: The starting player can make moves like "--++" or "+--+" or "++--". Let's say they choose "--++", then the opponent can only make "----", and the game ends. The starting player made the last move, so they win.
example_2.py โ€” Simple Losing Case
$ Input: currentState = "+"
โ€บ Output: false
๐Ÿ’ก Note: The starting player cannot make any moves (no consecutive "++" found). Since they cannot move, they lose immediately.
example_3.py โ€” Strategic Case
$ Input: currentState = "+++++"
โ€บ Output: true
๐Ÿ’ก Note: Starting player can flip positions 1-2 to get "--+++". No matter what the opponent does next, the starting player can force a win through optimal play.

Visualization

Tap to expand
Flip Game II - Game Theory Analysis"++++"Player 1 Turn--+++--+++--Player 2 TurnPlayer 2 TurnPlayer 2 Turn--------No moves leftPlayer 2 losesNo moves leftPlayer 2 losesNo moves availablePlayer 2 loses๐ŸŽฏ Game Theory Result: Player 1 WINS!Player 1 has multiple moves that guarantee victoryANY of these moves puts Player 2 in a losing position
Understanding the Visualization
1
Identify Moves
Find all positions where we can flip '++' to '--'
2
Explore Outcomes
For each move, recursively analyze if opponent can win
3
Apply Game Theory
Current player wins if ANY move puts opponent in losing position
4
Cache Results
Memoize to avoid recomputing the same game states
Key Takeaway
๐ŸŽฏ Key Insight: In game theory problems, a position is winning if there exists at least one move that puts your opponent in a losing position. Use memoization to efficiently explore the game tree!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * 2^n)

Same as memoized version but with constant factor improvements

n
2n
โš  Quadratic Growth
Space Complexity
O(2^n)

Hash table with optimized string storage

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค currentState.length โ‰ค 60
  • currentState[i] is either '+' or '-'
  • Both players play optimally
Asked in
Google 42 Facebook 38 Microsoft 25 Amazon 15
28.4K Views
Medium-High Frequency
~25 min Avg. Time
892 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