File: flip-game/solution.py

Date: 2026-06-06

Time: 16:52

Purpose

This file implements the solution to LeetCode 293 — Flip Game. It owns the game-move generation logic: given a board state (a string of + and - characters), it produces every possible next state by flipping exactly one pair of adjacent ++ into --.

Key Components

Solution.generatepossiblenext_moves(currentState: str) -> List[str]

The sole public method. Its contract:

Patterns

Sliding window of size 2: The loop at for i in range(len(currentState) - 1) examines every consecutive pair (i, i+1). This is the canonical approach for adjacent-pair scanning — O(n) time, O(n) space for results.

Immutable string construction via slicing: Each new state is built with currentState[:i] + "--" + currentState[i + 2:]. This avoids mutating the input and produces independent copies in the result list. Each slice-and-concat is O(n), giving O(n^2) total for the method in the worst case (all + characters).

Dependencies

Imports: typing.List — used only for the return type annotation.

Imported by: The flip-game/test_solution.py file tests this solution directly. The long list of other test files in Imported By is likely an artifact of the repo's shared test infrastructure importing Solution generically, not actual runtime dependencies on this specific module.

Flow

1. Initialize an empty result list.

2. Iterate i from 0 to len(currentState) - 2.

3. At each position, check if both currentState[i] and currentState[i+1] are +.

4. If so, construct a new string with those two characters replaced by -- and append it.

5. Return the accumulated list.

Invariants

Error Handling

None. The method trusts that currentState is a valid string. An empty string or single-character string naturally produces an empty result because range(len(s) - 1) yields an empty range (or range(-1) which is also empty). No exceptions are raised or caught.

Topics to Explore

Beliefs