> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Flip Game II Python Solution with Tests

> Tested Python solution for LeetCode 294 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 294, [Medium](/catalog/medium). Topics: [Memoization](/catalog/topics/memoization), [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming), [Backtracking](/catalog/topics/backtracking), [Game Theory](/catalog/topics/game-theory). [View on LeetCode](https://leetcode.com/problems/flip-game-ii/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 294   # by problem number
lcpy gen -s flip_game_ii   # by problem name
```

## 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.

**Follow up:** Derive your algorithm's runtime complexity.

### Examples

```
Input: currentState = "++++"
Output: true
Explanation: The starting player can guarantee a win by flipping the middle "++" to become "+--+".
```

```
Input: currentState = "+"
Output: false
```

### Constraints

* `1 <= currentState.length <= 60`
* `currentState[i]` is either `'+'` or `'-'`.
* There cannot be more than 20 consecutive `'+'`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flip_game_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flip_game_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2 * 2^n) where n is the string length, memoized in practice
    # Space: O(2^n)
    def can_win(self, current_state: str) -> bool:
        memo: dict[str, bool] = {}

        def can_win_from(state: str) -> bool:
            if state in memo:
                return memo[state]
            result = False
            for i in range(len(state) - 1):
                if state[i : i + 2] == "++":
                    next_state = state[:i] + "--" + state[i + 2 :]
                    if not can_win_from(next_state):
                        result = True
                        break
            memo[state] = result
            return result

        return can_win_from(current_state)
```

## Complexity

| Time | Space |
| - | - |
| O(n^2 \* 2^n) where n is the string length, memoized in practice | O(2^n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.