<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>

using namespace std;
class Solution {
public:
    bool checkPal(string s, int st, int end) {
        int i=st, j=end;
        while(i<j)
        {
            if(s[i++]!=s[j--])
                return false;
        }
        return true;
    }

    vector<vector<string>> partition(string s) {
        std::vector<std::vector<std::string>> ans;
        std::vector<std::vector<std::string>> tem;
        std::vector<std::string> temp, dummy;
        // cout<<s;
        int n = s.size();
        int i,j,k;
        // char c;
        // string dummy;
        if(n==0)
            return ans;
        if(n==1)
        {
            ans.push_back(temp);
            ans[0].push_back(s.substr(0,1));
            return ans;
        }
        // ans.push_back(temp);

        // backtrack(s, ans);
        
        i=0;
        for(j=i; j<n; j++)
        {
            dummy.clear();
            if(checkPal(s, i, j)) {
                dummy.push_back(s.substr(i,j+1-i));
                if(j+1 == n)
                    ans.push_back(dummy);
                tem = partition(s.substr(j+1-i));
                for(k=0; k<tem.size(); k++)
                {
                    temp = dummy;
                    temp.insert(temp.end(), tem[k].begin(), tem[k].end());
                    ans.push_back(temp);
                }
            }
        }
        return ans;
    }
};
190301