<p>Given a string <code>s</code>, partition <code>s</code> such that every <span data-keyword="substring-nonempty">substring</span> of the partition is a <span data-keyword="palindrome-string"><strong>palindrome</strong></span>. Return <em>all possible palindrome partitioning of </em><code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "aab"
<strong>Output:</strong> [["a","a","b"],["aa","b"]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "a"
<strong>Output:</strong> [["a"]]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 16</code></li>
	<li><code>s</code> contains only lowercase English letters.</li>
</ul>

class Solution:
    def partition(self, s: str) -> List[List[str]]:

        def check_palindrome(s):
            return s == s[::-1]
        
        n = len(s)
        ans = [[] for i in range(n)]

        for i in range(n):
            for j in range(0,i+1):
                if check_palindrome(s[j:i+1]):
                    if(j!=0):
                        for sublist in ans[j-1]:
                            ans[i].append(sublist + [s[j:i+1]])
                    else:
                        ans[i].append([s[j:i+1]])
            # print (ans[i])

        return ans[n-1]

        # n = len(s)
        
        # # Step 1: Create the DP table to check for palindromes
        # dp = [[False] * n for _ in range(n)]
        
        # # All substrings of length 1 are palindromes
        # for i in range(n):
        #     dp[i][i] = True
        
        # # Check for palindromes of length 2 or more
        # for length in range(2, n + 1):  # length of substring
        #     for i in range(n - length + 1):  # starting index
        #         j = i + length - 1  # ending index
        #         if s[i] == s[j]:
        #             if length == 2 or dp[i+1][j-1]:
        #                 dp[i][j] = True
        
        # # Step 2: Backtracking function to find all palindrome partitions
        # def backtrack(start, current_partition, result):
        #     if start == n:  # Base case: If we've partitioned the entire string
        #         result.append(current_partition[:])  # Add the current partition to the result
        #         return
            
        #     for end in range(start, n):
        #         if dp[start][end]:  # If s[start:end+1] is a palindrome
        #             current_partition.append(s[start:end+1])  # Add palindrome substring to the partition
        #             backtrack(end + 1, current_partition, result)  # Recurse on the remaining substring
        #             current_partition.pop()  # Backtrack to try other partitions
        
        # # Initialize the result list and start backtracking
        # result = []
        # backtrack(0, [], result)
        # return result
            
32885