<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 ifPalindrome(string &s)
    {
        int n= s.size(), i= 0, j= n-1;
        while(i < j)
        {
            if(s[i] != s[j])
                return false;
            i++, j--;
        }
        return true;
    }
    
    void doPartition(string &s, vector<string> base, int ind, vector<vector<string>> &ans)
    {
        int n= s.size();

        for(int i= 1;i<n;i++)
        {
            string s1= s.substr(0, i);
            string s2= s.substr(i, n-i);

            bool flag1= ifPalindrome(s1);
            bool flag2= ifPalindrome(s2);

            if(flag1 && flag2)
            {
                vector<string> nBase= base;
                nBase.push_back(s1);
                nBase.push_back(s2);
                ans.push_back(nBase);
            }
                
            if(s1.size() > 0 && flag1)
            {
                vector<string> nBase= base;
                nBase.push_back(s1);
                doPartition(s2, nBase, i, ans);
            }
        }
    }
    
    vector<vector<string>> partition(string s)
    {
        vector<vector<string>> ans;
        vector<string> temp = {};

        if(ifPalindrome(s))
            ans.push_back({s});

        doPartition(s, temp, 0, ans);
        return ans;
    }
};
183929