<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:
    vector<vector<string>> partitions;

    /*
    Approach 1: I think this is the same as approach 2 in essence, just more confusing
    
    void backtrack(string s, vector<string> sofar, int idx, int lastidx) {
        if (idx == s.size()) {
            if (lastidx == idx - 1) {
                partitions.push_back(sofar);
            }
            
            return;
        }

        // Look for odd palindrome including lastidx extending out from idx
        int left = idx;
        int right = idx;
        while (left > lastidx && right < s.size() && s[left] == s[right]) {
            --left;
            ++right;
        }
        if (left == lastidx && right <= s.size()) {
            string pal = s.substr(lastidx + 1, right - left - 1);
            sofar.push_back(pal);
            backtrack(s, sofar, right, right - 1);
            sofar.pop_back();
        }
        
        // Look for even palindrome including lastidx extending out from idx
        left = idx - 1;
        right = idx;
        while (left > lastidx && right < s.size() && s[left] == s[right]) {
            --left;
            ++right;
        }
        if (left == lastidx && right - left > 2 && right <= s.size()) {
            string pal = s.substr(lastidx + 1, right - left - 1);
            sofar.push_back(pal);
            backtrack(s, sofar, right, right - 1);
            sofar.pop_back();
        }
        if (right == s.size()) {
            return;
        }

        // Don't include this character
        backtrack(s, sofar, idx + 1, lastidx);
    }

    vector<vector<string>> partition(string s) {
        backtrack(s, vector<string>(), 0, -1);
        return partitions;
    }*/

    bool isPal(string s, int i, int j) {
        cout << s.substr(i, j - i + 1) << endl;
        int left = i;
        int right = j;
        while (left < right) {
            if (s[left] != s[right]) {
                return false;
            }
            ++left;
            --right;
        }
        return true;
    }

    void backtrack(string s, vector<string> sofar, int idx) {
        if (idx == s.size()) {
            partitions.push_back(sofar);
            return;
        }

        // For all substrings starting from this idx, check if palindrome
        for (int j = idx; j < s.size(); ++j) {
            if (isPal(s, idx, j)) {
                sofar.push_back(s.substr(idx, j - idx + 1));
                backtrack(s, sofar, j + 1);
                sofar.pop_back();
            }
        }
        return;
    }

    vector<vector<string>> partition(string s) {
        backtrack(s, vector<string>(), 0);
        return partitions;
    }

};



/*
aab

for each character, go outwards from that character


aababcbaccd

a a b a b

a b c b a c c d
a a b a b c b a 

backtrack(s, sofar, idx, lastchosen)




*/

177556