Problem
You are playing a Flip Game with your friend.
You are given a string currentState
that contains only '+'
and '-'
. You and your friend take turns to flip two consecutive "++"
into "--"
. The game ends when a person can no longer make a move, and therefore the other person will be the winner.
Return true
if the starting player can guarantee a win, and false
otherwise.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Backtracking
To determine if the starting player can guarantee a win in the Flip Game, we will use a backtracking approach. The main idea is to recursively simulate the game: at each step, we flip two consecutive “++” into “–” and then check if the opponent can win from this new state. If there exists at least one move that results in a win for the starting player (i.e., forces the opponent to lose in the subsequent moves), then the starting player can guarantee a win.
Approach
- Recursive Backtracking:
- Iterate through the string
currentState
to find all positions where “++” can be flipped to “–”. - For each valid move, flip “++” to “–” and recursively check if the opponent (starting player of the next turn) can guarantee a win from the new state.
- If there is at least one move that causes all subsequent moves of the opponent to result in a loss, return
true
.
- Iterate through the string
- Memoization:
- Use memoization to store the results of previously computed states to avoid redundant computations and speed up the process.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n!)
, in the worst case, as each move can generate multiple recursive calls and the number of valid moves decreases factorially. However, with memoization, it improves significantly. - 🧺 Space complexity:
O(n!)
, in the worst case due to the depth of the recursion tree and memoization storage.