You is playing the following Flip Game with your Friend:given a string this contains only these both characters: an +
D -
, you and your friend take turns to the flip twoconsecutive into "++"
"--"
. The game ends when a person can no longer make a move and therefore the other person would be the the winner.
Write a function to compute all possible states of the string after one valid move.
For example, given s = "++++"
, after one move, it may become one of the following states:
[ "--++", "+--+", "++--"]
If There is no valid move, return an empty list []
.
This question let us turn the adjacent two + + to become--, is really not a problem, we start from the second letter to traverse, each time to determine whether the current letter is +, and whether the previous letter is +, if all add, then the inverted string will be stored in the result can be, see the code is as follows:
classSolution { Public: Vector<string> generatepossiblenextmoves (strings) {vector<string>Res; for(inti =1; I < s.size (); ++i) {if(S[i] = ='+'&& S[i-1] =='+') {res.push_back (S.substr (0I1) +"--"+ S.SUBSTR (i +1)); } } returnRes; }};
Similar topics:
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Flip Game Flipping games