Recursive python solution


  • 1
    A

    class Solution(object):

    def canWin(self, s):
    
        poss = self.getposs(s, False)
        return poss
        
    def getposs(self, s, poss):
    
        for i in range(len(s)-1):
            if s[i:i+2] != '++':
                continue
            else:
                poss = poss or not self.getposs(s[:i] + '--' + s[i+2:], poss)
        return poss

  • 2
    Z

    Why not just one function

    def canWin(self, s):
        for i in range(len(s)-1):
            if s[i:i+2] == "++":
                if not self.canWin(s[:i]+"--"+s[i+2:]):
                    return True
        return False

Log in to reply
 

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