Elegant recursive Python solution


  • 0
    B
    class Solution(object):
    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        """
        if len(s) == 0:
            return [[]]
        result = []
        for i in xrange(len(s)):
            if self.isPalindrome(s[:i+1]):
                for l in self.partition(s[i+1:]):
                    result.append([s[:i+1]] + l)
        return result
                
    
    def isPalindrome(self, s):
        return s == s[::-1]

Log in to reply
 

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