<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 check(string &t)
    {
        int beg=0;
        int end=t.size()-1;
        while(beg<end)
        {
            if(t[beg]==t[end])
            {
                beg++;
                end--;
            }
            else
            return false;
        }
        return true;
    }
    void solve(int index,string t,vector<string> &v,string s,map<char,int> m,int flag,vector<vector<string>> &ans)
    {
        if(index==s.size())
        {
           if(t.size()==0)
           ans.push_back(v);
           return;
        }
        if(t.size()==0)
        {
            t.push_back(s[index]);
            m[s[index]]++;
            solve(index+1,t,v,s,m,1,ans);
            m.clear();
            v.push_back(t);
            t.clear();
            solve(index+1,t,v,s,m,-1,ans);
            v.pop_back();

        }
        else
        {
            if(flag==1)
            {
                if(m.size()>1)
                {
                    t.push_back(s[index]);
                    m[s[index]]++;
                    solve(index+1,t,v,s,m,0,ans);
                }
                else
                {
                    if(s[index]==t[0])
                    {
                        t.push_back(s[index]);
                        m[s[index]]++;
                        solve(index+1,t,v,s,m,1,ans);
                        m.clear();
                        v.push_back(t);
                        t.clear();
                        solve(index+1,t,v,s,m,-1,ans);
                        v.pop_back();
                    }
                    else
                    {
                        t.push_back(s[index]);
                        m[s[index]]++;
                        solve(index+1,t,v,s,m,0,ans);
                    }
                }
            }
            else
            {
                t.push_back(s[index]);
                m[s[index]]++;
                bool x=check(t);
                if(x==true)
                {
                    solve(index+1,t,v,s,m,1,ans);
                    m.clear();
                    v.push_back(t);
                    t.clear();
                    solve(index+1,t,v,s,m,-1,ans);
                    v.pop_back();
                }
                else
                {
                    solve(index+1,t,v,s,m,0,ans);
                }
            }
        }

        
        

    }
    vector<vector<string>> partition(string s) {
        string t;
        vector<string> v;
        vector<vector<string>> ans;
        map<char,int> m;
        solve(0,t,v,s,m,-1,ans);
        return ans;
    }
};
187115