<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>> result;
        vector<string> current{{s[0]}};
        backtracking(s, 1, current, result);
        return result;
    }

private:
    void backtracking(const string& s, int index, vector<string>& current,
                      vector<vector<string>>& result) {
        if (index >= s.size()) {
            if (isPalindrome(current.back())) {
                result.push_back(current);
            }
            return;
        }

        // add letter as a begining of new partition - previous partition must
        // be palindrome
        if (isPalindrome(current.back())) {
            current.push_back({s[index]});
            backtracking(s, index + 1, current, result);
            current.pop_back();
        }

        // add letter to previous partition - not necesarilly palindrome
        current.back().push_back(s[index]);
        backtracking(s, index + 1, current, result);
        current.back().pop_back();
    }

    bool isPalindrome(string word) {
        int l = 0;
        int r = word.size() - 1;
        while (l <= r) {
            if (word[l] != word[r]) {
                return false;
            }
            l++;
            r--;
        }
        return true;
    }
};
69224