Palindrome Partitioning

Solution Code
class Solution:
    def partition(self, s: str) -> List[List[str]]:
        res, part = [], []
        def is_palindrome(s, left, right):
            while left < right:
                if s[left] != s[right]: return False
                left, right = left + 1, right - 1
            return True
        def dfs(i):
            if i >= len(s):
                res.append(part.copy())
                return
            for j in range(i, len(s)):
                if is_palindrome(s, i, j):
                    part.append(s[i:j+1])
                    dfs(j + 1)
                    part.pop()
        dfs(0)
        return res
Video GuideLeetcode Daily

Time Complexity

O(N 2^N)

Space Complexity

O(N)