<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:
    vector<vector<string>> partition(string s) {
        vector<vector<string>> answer;
        vector<string> current;
        current.push_back(s);
        if (checkPalindrome(s)){
            answer.push_back(current);
        }
        backtracking(current, answer);
        return answer;
    }

    void backtracking(vector<string> current, vector<vector<string>>& answer) {
        // If Left substring is not palindrome. Dont evaluate tree further
        if (!checkPalindrome(current[0]) && current.size() > 1) {
            return;
        }

        bool allPalindromes = true;

        for(int i=1; i<current.size(); i++) {
            allPalindromes = allPalindromes && checkPalindrome(current[i]);
        }

        if (allPalindromes && current.size() > 1) {
            answer.push_back(current);
        }

        //Leaf node reached
        // No More division possible
        if (current.back().size() == 1) {
            return;
        }

        string last = current.back();
        current.pop_back();

        for(int i =1; i<last.size(); i++) {
            string substr1 = last.substr(0, i);
            string substr2 = last.substr(i, last.size() - i);
            current.push_back(substr1);
            current.push_back(substr2);
            backtracking(current, answer);
            current.pop_back();
            current.pop_back();
        }

    }

    bool checkPalindrome(string& check) {
        int left = 0;
        int right = check.size()-1;

        while(left < right) {
            if (check[left] != check[right]) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
};
101086