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 determine if the starting player can guarantee a win.
For example, given s = "++++"
, return true. The starting player can guarantee a win by flipping the middle to "++"
become "+--+"
.
Follow up:
Derive your algorithm ' s runtime complexity.
Problem Solving Ideas:
Backtracking
Java Code
Public class Solution { publicboolean Canwin (String s) { for (int i = 0; I < S.length ()-1; i++) { if(S.charat (i) = = ' + ' && s.charat (i+1) = = ' + ' &&!canwin (s.substring ( 0,i) + "--" + s.substring (i+2)) {returntrue; } } return false ; }}
Reference:
1. Https://leetcode.com/discuss/64330/4-line-java-solution
2. Https://leetcode.com/discuss/64357/memoization-3150ms-130ms-44ms-python
Leetcode Flip Game II