<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>

"""
before state = ""
    after state = "a"
"""

from typing import List

class Solution:
    def partition(self, s: str) -> List[List[str]]:
        def is_palindrome(sub: str) -> bool:
            return sub == sub[::-1]  # Check if a substring is a palindrome

        def backtrack(curr_path: List[str], remaining_chars: str):
            if not remaining_chars:  # Base case: no more characters to process
                result.append(curr_path.copy())  # Add the current partition to result
                return
            
            # Try every possible substring from the start of remaining_chars
            for i in range(1, len(remaining_chars) + 1):
                substring = remaining_chars[:i]
                if is_palindrome(substring):
                    curr_path.append(substring)  # Include the palindrome substring in the partition
                    # Recursive call with updated partition and remaining characters
                    backtrack(curr_path, remaining_chars[i:])
                    curr_path.pop()  # Backtrack: remove the last added substring

        result = []  # List to store all possible partitions
        backtrack([], s)  # Start the recursion with an empty partition and the full string
        return result
34108