<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 s;
int dp[18][18];
vector<vector<string>> dp1[18][18];
vector<vector<string>> pp;
    vector<vector<string>> get(int i, int j) {
        if(i>j) return {{}};
        if(i==j) return {{s.substr(i,j-i+1)}};
        if(dp1[i][j] != pp) return dp1[i][j];
        vector<vector<string>> ans;
        for(int k=i;k<=j;k++) {
            if(dp[k-i+1][k]) {
                vector<vector<string>> a1 = get(k+1, j);
                for(auto it: a1) {
                    vector<string> b1={s.substr(i,k-i+1)};
                    b1.insert(b1.end(), it.begin(), it.end());
                    ans.push_back(b1);
                }
            }
        }
        return dp1[i][j]=ans;
    }
    vector<vector<string>> partition(string s1) {
        s = s1;
        pp={{"-1"}};
        memset(dp,0,sizeof(dp));
        for(int i = 0;i<18;i++) {
            for(int j=0;j<18;j++) {
                dp1[i][j]=pp;
            }
        }
        int n = s.size();
        for(int l= 1;l<=n;l++) {
            for(int i =l-1;i<n;i++) {
                if(l==1 || l==2) {
                    if(s[i]==s[i-l+1]) dp[l][i]=1;
                } else {
                    if(s[i]==s[i-l+1] && dp[l-2][i-1]) dp[l][i]=1;
                }
            }
        }
        return get(0,n-1);
    }
};
301820