<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 {
public:
    string partS(string s, int start, int end) {
        string result = "";
        string rev = "";

        // Extract substring from 'start' to 'end-1'
        for (int i = start; i < end; i++) {
            result += s[i];
        }

        // Reverse the substring 'result'
        for (int i = end - 1; i >= start; i--) {
            rev += s[i];
        }

        // Check if 'result' is a palindrome
        if (result == rev) {
            return result;
        } else {
            return "-1";
        }
    }

    void partStrings(int last, int i, int n, string s, vector<string> curr, vector<vector<string>> &result) {
        if(i >= n) {
            string partedS = partS(s, last, i);
            if(partedS != "-1") {
                // cout << last << " " << i << endl; 
                curr.push_back(partedS);
                result.push_back(curr);
                curr.pop_back();
            }
            return;
        }
        //partition
        string partedS = partS(s, last, i);
        if(partedS != "-1") {
            // cout << last << " " << i << endl; 
            curr.push_back(partedS);
            partStrings(i, i+1, n, s, curr, result);
            curr.pop_back();
        }


        //no partition
        partStrings(last, i+1, n, s, curr, result);

    }
    vector<vector<string>> partition(string s) {
        int n = s.size();
        vector<vector<string>> result;
        partStrings(0, 1, n, s, {}, result);
        return result;

    }
};
301820