<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:
    bool isPan(string &s)
    {
        //cout<<"s="<<s<<endl;
        for(int i=0;i<s.size()/2;i++)
        {
            if(s[i]!=s[s.size()-i-1])
            {
                return false;
            }
        }
        //cout<<"return true"<<endl;
        return true;
    }
    vector<vector<string>> partition(string s) {
        //DP1 - to store the string arrays of all palindrome, DP2 - to store the string arrays of all palindrome except the last string
        if(s.size()==1)
        {
            return vector<vector<string>> {vector<string>{s}};
        }
        vector<vector<string>> DP1 {vector<string>{string(1,s[0])}}, DP2{};
        
        for(int i=1;i<s.size()-1;i++)
        {
            int len1=DP1.size();
            int len2=DP2.size();
            
            for(int j=0;j<len1;j++)
            {
                vector<string> vs=DP1[j];
                vs[vs.size()-1].push_back(s[i]);
                if(isPan(vs[vs.size()-1]))
                {
                    DP1.push_back(vs);
                }
                else
                {
                    DP2.push_back(vs);
                }
            }
            for(int j=0;j<len1;j++)
            {
                DP1[j].push_back(string(1,s[i]));
            }
            
            for(int j=0;j<len2;j++)
            {
                DP2[j][DP2[j].size()-1].push_back(s[i]);
                if(isPan(DP2[j][DP2[j].size()-1]))
                {
                    DP1.push_back(DP2[j]);
                }
                else
                {
                    DP2.push_back(DP2[j]);
                }
            }
            DP2.erase(DP2.begin(),DP2.begin()+len2);
        }
        
        int len1=DP1.size();
        int len2=DP2.size();
        
        for(int j=0;j<len1;j++)
        {
            vector<string> vs=DP1[j];
            vs[vs.size()-1].push_back(s[s.size()-1]);
            if(isPan(vs[vs.size()-1]))
            {
                DP1.push_back(vs);
            }
        }
        for(int j=0;j<len1;j++)
        {
            DP1[j].push_back(string(1,s[s.size()-1]));
        }

        for(auto & vs:DP2)
        {
            vs[vs.size()-1].push_back(s[s.size()-1]);
            if(isPan(vs[vs.size()-1]))
            {
                DP1.push_back(vs);
            }
        }
        return DP1;



    }
};
91528