mengyushi / LeetCode

4 stars 0 forks source link

131. Palindrome Partitioning #256

Open mengyushi opened 4 years ago

mengyushi commented 4 years ago
class Solution:
    def partition(self, s: str) -> List[List[str]]:
        '''
        Backtracking

        '''

        res = []
        N = len(s)
        for i in range(1,N+1):
            if s[:i] == s[:i][::-1]:
                # s[:i] is palindrome
                res+=[[s[:i]]+rest for rest in self.partition(s[i:])]

        if not res:
            return [[]]

        return res