Flip Game
The problem
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 [].
Input: currentState = "++++++" Output: ["--++++", "+--+++", "++--++", "+++--+", "++++--"]
Input: currentState = "++--++" Output: ["--.--++", "++----"]
Input: currentState = "-++-"
- 1 <= currentState.length <= 500
- currentState[i] is either '+' or '-'.
cpp
class Solution {
public:
vector<string> generatePossibleNextMoves(string s) {
// Your code goes here
}
};java
class Solution {
public List<String> generatePossibleNextMoves(String s) {
// Your code goes here
}
}python
class Solution:
def generatePossibleNextMoves(self, s: str):
# Your code goes herejavascript
class Solution {
generatePossibleNextMoves(s) {
// Your code goes here
}
}csharp
public class Solution {
public IList<string> GeneratePossibleNextMoves(string s) {
}
}go
func generatePossibleNextMoves(s string) []string {
}Stuck? Show a way to structure it+
- 01Clarify whether this is the one-move or two-player version
- 02Enumerate every consecutive ++ pair
- 03For game play, recurse after a flip and negate the child result
- 04Memoize states and restore mutable state after each trial
Reference answer
Then expect these follow-ups
Can you optimize with Sprague-Grundy theory?
Tests: follow-up reasoning
What is the complexity of the memoized search?
Tests: complexity analysis
Free to read · better with Enzo
Practice this out loud with Enzo
Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.
Next question