Python standard backtracking


  • 0
    W
    def partition(self, s):    
        ret = []
        self.dfs(s, 0, [], ret)
        return ret
        
    def dfs(self, s, index, cur, ret):
        if index >= len(s):
            ret.append(cur[::])
            return
        for i in range(index, len(s)):
            temp = s[index:i+1]
            cur.append(temp)
            if temp == temp[::-1]: 
                self.dfs(s, i+1, cur, ret)
            cur.pop()

Log in to reply
 

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