Python Clean and Fast Solution


  • 0
    D
    class Solution(object):
        def partition(self, s):
            """
            :type s: str
            :rtype: List[List[str]]
            """
            validated = lambda x: x == x[::-1]
            ans = []
            n = len(s)
            def collect(i, curr):
                if i >= n: 
                    ans.append(curr)
                    return
                for j in range(i+1, n+1):
                    if validated(s[i:j]):
                        collect(j, curr+[s[i:j]])
            collect(0, [])
            return ans

Log in to reply
 

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