<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) {
    int n = s.length();
    vector<vector<int>> valids(n, vector<int>(1,1));
    for (int i = 2; i <= n; i++){
        for (int j = 0; j <= n-i; j++){
            bool works = true;
            for (int k = 0; k < i/2 && works; k++) if (s[k+j] != s[j+i-1-k]) works = false;
            if (works) valids[j].push_back(i);
        }
    }
    vector<vector<string>> ans;
    int ind = 0;
    vector<int> nums(n, 0);
    vector<string> current;
    while (ind >= 0){
        if (ind == n){
            ans.push_back(current);
            ind -= current.back().length();
            nums[ind]++;
            current.pop_back();
        }
        else if (nums[ind] >= valids[ind].size()){
            if (ind == 0) break;
            nums[ind] = 0;
            ind -= current.back().length();
            current.pop_back();
            nums[ind]++;
        } else {
            current.push_back(s.substr(ind, valids[ind][nums[ind]]));
            ind += valids[ind][nums[ind]];
        }
    }
    return ans;
    }
};
53293