Flip Game - Problem
Flip Game Challenge

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
Light Switch Panel - Flip GameOriginal Panel: +++++ON+ON+ON+ONPossible Moves:Move 1: Flip switches 0-1--++Result: "--++"Move 2: Flip switches 1-2+--+Result: "+--+"Move 3: Flip switches 2-3++--Result: "++--"Algorithm Steps1. Scan from left to right2. Find consecutive ON switches3. For each pair, create new state4. Flip the pair to OFF position5. Add to results listTime: O(n²) | Space: O(nƗk)Final Answer: ["--++", "+--+", "++--"]
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

n
2n
⚠ Quadratic Growth
Space Complexity
O(n * k)

Where k is the number of valid moves, each requiring O(n) space for the new string

n
2n
⚔ Linearithmic Space

Constraints

  • 1 ≤ currentState.length ≤ 500
  • currentState[i] is either '+' or '-'
  • The game requires at least 2 characters to make a valid move
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
28.4K Views
Medium Frequency
~15 min Avg. Time
850 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