The Flip Game problem on Leetcode asks you to generate all possible states of a string after flipping any pair of consecutive '+'
characters to '--'
.
You are given a string s
consisting only of the characters '+'
and '-'
. Your task is to return all possible strings that can be obtained by flipping any occurrence of "++"
into "--"
in s
.
Key constraints:
'+'
at a time.s = "++++"
["--++","+--+","++--"]
The core idea is to identify every pair of consecutive '+'
characters in the string and flip them to '--'
to create a new state.
The brute-force way would be to check every possible pair, flip it, and add the resulting string to the output. This is feasible because each flip is independent and we only need to perform one flip per result.
There is no need for recursion or backtracking, since we only flip one pair at a time. We simply scan left-to-right, and whenever we see "++"
, we flip it and store the new string.
Optimization isn't needed for this problem since the constraints are small, but we should be careful to only flip one pair per result and not to miss any possible flip.
Let's break down the steps to solve the problem:
i
from 0
to len(s) - 2
.
s[i] == '+' and s[i+1] == '+'
. This identifies a valid flip location.
s[i]
and s[i+1]
are replaced with '--'
, and the rest of the string remains the same.
Let's use the input s = "++++"
as an example.
Step-by-step:
s[0] == '+' and s[1] == '+'
. Flip these to get "--++"
.
s[1] == '+' and s[2] == '+'
. Flip these to get "+--+"
.
s[2] == '+' and s[3] == '+'
. Flip these to get "++--"
.
["--++", "+--+", "++--"]
Brute-force approach:
The Flip Game problem is an exercise in string manipulation and careful iteration. By scanning for every pair of consecutive '+'
characters and flipping them one at a time, we generate all possible next states. The solution is elegant in its simplicity: no recursion or extra data structures are needed, just a simple loop and string slicing. This makes the code easy to understand and efficient for the problem's constraints.
class Solution:
def generatePossibleNextMoves(self, s: str):
res = []
for i in range(len(s) - 1):
if s[i] == '+' and s[i + 1] == '+':
flipped = s[:i] + '--' + s[i+2:]
res.append(flipped)
return res
class Solution {
public:
vector<string> generatePossibleNextMoves(string s) {
vector<string> res;
for (int i = 0; i < (int)s.size() - 1; ++i) {
if (s[i] == '+' && s[i+1] == '+') {
string flipped = s.substr(0, i) + "--" + s.substr(i+2);
res.push_back(flipped);
}
}
return res;
}
};
class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> res = new ArrayList<>();
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == '+' && s.charAt(i+1) == '+') {
String flipped = s.substring(0, i) + "--" + s.substring(i+2);
res.add(flipped);
}
}
return res;
}
}
/**
* @param {string} s
* @return {string[]}
*/
var generatePossibleNextMoves = function(s) {
const res = [];
for (let i = 0; i < s.length - 1; i++) {
if (s[i] === '+' && s[i+1] === '+') {
let flipped = s.slice(0, i) + '--' + s.slice(i+2);
res.push(flipped);
}
}
return res;
};