Java recursion, no memoization.


  • 0
    Z
    public boolean canWin(String s) {
        int idx = -1;
    
        while(-1 != (idx = s.indexOf("++",idx + 1))){
            String t = s.substring(0, idx) + "--" + s.substring(idx + 2);
            if(!canWin(t)) // elegant. if opponent cannot win, when I win.
                return true;
        }
    
        return false;
    }

Log in to reply
 

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