A fast and easy to understand java solution


  • 0
    Z

    '''
    public class Solution {

    Map<String , Boolean> dp = new HashMap<>();
    
    public boolean canWin(String s) {
       
        if(dp.containsKey(s)) {
            return dp.get(s);
        }
        
        int i, len = s.length();
        char[] chars = s.toCharArray();
        
        for(i=0;i<len-1;i++) {
            if(chars[i] == '+' && chars[i+1] == '+') {
                chars[i] = '-';
                chars[i+1] = '-';
                
                if(!canWin(new String(chars))) {
                    dp.put(s , true);
                    return true;
                }
                
                chars[i] = '+';
                chars[i+1] = '+';
            }
        }
        
        dp.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.