Java 95 + % solution


  • 0
    V
    class Solution {
        public boolean canWin(String s) {
            return canWin(s.toCharArray(), new HashMap<String, Boolean>());
        }
        
        private boolean canWin(char[] A, Map<String, Boolean> memo) {
            String s = new String(A);
            if (memo.containsKey(s)) return memo.get(s);
            
            for (int i = 0; i < A.length - 1; i++) {
                if (A[i] == '+' && A[i + 1] == '+') {
                    A[i + 1]  = A[i] = '-';
                    boolean canIwin = !canWin(A, memo);
                    A[i + 1] = A[i] = '+';
                    if (canIwin) {
                        memo.put(s, true);
                        return true;
                    }
                }
            }
            
            memo.put(s, false);
            return false;
        }
    }
    

Log in to reply
 

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