Short Java backtracking solution


  • 0
    E
    public boolean canWin(String s) {
            for(int i = 1; i < s.length(); i++) {
                if(s.charAt(i) == '+' && s.charAt(i - 1) == '+') {
    //if this round the string can be flipped and the next round it can not, you win
                   if(!canWin(s.substring(0, i - 1) + "--" + s.substring(i + 1, s.length()))) return true;
                }
            }
            return false;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.