<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>> partition(string s) {
        // find all palindromes first (?) and their start and ends
        vector<vector<bool>> isP(s.size(), vector<bool>(s.size(), false));
        for (int i = 0; i < s.size(); i++) {
            isP[i][i] = true;
        }
        for (int i = s.size(); i >= 0; i--) {
            for (int j = i+1; j < s.size(); j++) {
                // account for even and odd?
                if (i+1 <= j-1) {
                    isP[i][j] = s[i] == s[j] && isP[i+1][j-1];
                } else {
                    isP[i][j] = s[i] == s[j];
                }
            }
        }

        vector<vector<string>> out;
        // basically BFS/DFS
        stack<pair<int, vector<string>>> stack;
        stack.push({0, vector<string>()});
        while (!stack.empty()) {
            pair<int, vector<string>> curr = stack.top();
            stack.pop();
            if (curr.first == s.size()) {
                out.push_back(curr.second);
                continue;
            }
            for (int i = curr.first; i < s.size(); i++) {
                // search for possible next palindrome
                if (isP[curr.first][i]) {
                    vector<string> copy = curr.second;
                    copy.push_back(s.substr(curr.first, i-curr.first+1));
                    stack.push({i+1, copy});
                    // cout << curr.first << " " << i << " " << s.substr(curr.first, i-curr.first+1) << endl;
                }
            }
        }
        return out;
    }
};
263585