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 all possible states of the string currentState after one valid move. You may return the answer in any order. If there is no valid move, return an empty list [].
Example 1:
Input: currentState = "++++" Output: ["--++","+--+","++--"]
Example 2:
Input: currentState = "+" Output: []
Constraints:
1 <= currentState.length <= 500currentState[i] is either '+' or '-'.Problem Overview: You are given a string containing only '+' and '-'. A valid move flips any consecutive "++" into "--". Return all possible strings you can generate after making exactly one valid move.
Approach 1: Brute Force Pair Check (O(n^2) time, O(n) space)
Scan the string and examine every pair of adjacent characters. Whenever you find "++", construct a new string where that pair becomes "--". Because strings are immutable in most languages, creating the new string requires concatenating the prefix, the flipped pair, and the suffix. Each creation costs O(n), and you may check up to n positions, giving O(n^2) time overall with O(n) extra space per generated state. This approach is straightforward and commonly used when practicing basic string manipulation problems.
Approach 2: Traversal + Simulation (O(n^2) time, O(n) space)
Traverse the string once and simulate the flip when a valid pair appears. Instead of rebuilding the string using multiple substrings each time, convert the string to a mutable character array (or temporarily modify characters). When you encounter "++", flip the two characters to '-', record the resulting string, and then revert the change before continuing the traversal. This technique reduces repeated substring operations and keeps the logic clean. The scan itself is O(n), but each generated state still requires building a string of length n, so the total time remains O(n^2). The idea is a typical simulation pattern combined with simple traversal over a string.
Recommended for interviews: Interviewers expect the traversal + simulation solution. It demonstrates that you can iterate through the string, detect valid patterns, and generate new states without unnecessary work. Starting with the brute force explanation shows clear reasoning, while implementing the simulation version proves you can write efficient string manipulation code under interview constraints.
We traverse the string. If the current character and the next character are both +, we change these two characters to -, add the result to the result array, and then change these two characters back to +.
After the traversal ends, we return the result array.
The time complexity is O(n^2), where n is the length of the string. Ignoring the space complexity of the result array, the space complexity is O(n) or O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Check | O(n^2) | O(n) | Simple implementation when learning basic string scanning and result generation |
| Traversal + Simulation | O(n^2) | O(n) | Preferred interview solution with cleaner logic and minimal repeated substring operations |
LeetCode 293, "Flip Game" • Mastering Programming • 971 views views
Watch 9 more video solutions →Practice Flip Game with our built-in code editor and test cases.
Practice on FleetCode