Flip Game - Problem
Flip Game Challenge
You're playing an exciting game with your friend where you take turns flipping consecutive
Your task: Given a string
A valid move: Find any two consecutive
Example: If the current state is
Return all possible states after one move, or an empty list if no moves are possible.
You're playing an exciting game with your friend where you take turns flipping consecutive
++ symbols to -- in a string. Imagine you have a string like "++++" - you can flip any two consecutive plus signs to minus signs in one move.Your task: Given a string
currentState containing only '+' and '-' characters, find all possible states after making exactly one valid move.A valid move: Find any two consecutive
"++" and flip them to "--"Example: If the current state is
"++++", you can make moves at positions 0-1, 1-2, or 2-3, resulting in states: ["--++", "+--+", "++--"]Return all possible states after one move, or an empty list if no moves are possible.
Input & Output
example_1.py ā Basic Case
$
Input:
currentState = "++++"
āŗ
Output:
["--++", "+--+", "++--"]
š” Note:
There are three consecutive '++' pairs at positions (0,1), (1,2), and (2,3). Each can be flipped to '--' to create a new valid state.
example_2.py ā Mixed Characters
$
Input:
currentState = "+"
āŗ
Output:
[]
š” Note:
No consecutive '++' pairs exist, so no valid moves are possible. Return empty list.
example_3.py ā Already Flipped
$
Input:
currentState = "+-++"
āŗ
Output:
["+---"]
š” Note:
Only one consecutive '++' pair exists at positions (2,3), which can be flipped to create "+---".
Visualization
Tap to expand
Understanding the Visualization
1
Identify Targets
Scan the panel to find all pairs of adjacent ON switches
2
Generate States
For each valid pair, create a new panel state by flipping them OFF
3
Collect Results
Gather all possible panel configurations after one move
Key Takeaway
šÆ Key Insight: This is a generation problem where we must find ALL possible states, so we cannot avoid checking every position. The single-pass approach is optimal.
Time & Space Complexity
Time Complexity
O(n²)
We iterate through n positions, and for each valid move, we create a new string of length n
ā Quadratic Growth
Space Complexity
O(n * k)
Where k is the number of valid moves, each requiring O(n) space for the new string
ā” Linearithmic Space
Constraints
- 1 ⤠currentState.length ⤠500
- currentState[i] is either '+' or '-'
- The game requires at least 2 characters to make a valid move
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code