Simple Python with cache O(n)


  • -1
    L

    from collections import defaultdict
    class Solution(object):
    def canWin(self, s):
    """
    :type s: str
    :rtype: bool
    """
    if len(s)<2:
    return False
    perm=self.dic=defaultdict(bool)
    return self._canWin(s,perm)
    def _canWin(self,s,perm):
    if s in perm: return perm[s]
    for i in xrange(len(s)-1):
    if s[i:i+2]=="++":
    if not self._canWin(s[:i]+"--"+s[i+2:],perm):
    perm[s]=True
    return True
    perm[s]=False
    return False


Log in to reply
 

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