Two methods dfs backtracking and dfs backtracking with dp

    1. class Solution:
    2. def partition(self, s: str) -> List[List[str]]:
    3. #backtracking
    4. def dfs(start, s, curr, result):
    5. if start >= len(s):
    6. ans = [i for i in curr]
    7. result.append(ans)
    8. for end in range(start, len(s)):
    9. if isPalindrome(s, start, end):
    10. curr.append(s[start:end+1])
    11. dfs(end+1, s, curr, result)
    12. curr.pop()
    13. def isPalindrome(s, start, end):
    14. while start <= end and s[start] == s[end]:
    15. start += 1
    16. end -= 1
    17. if end - start <= 0:
    18. return True
    19. else:
    20. return False
    21. if not s:
    22. return []
    23. result = []
    24. dfs(0, s, [], result)
    25. return result