<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) {
        ans.clear();
        vector<string> cur_partition;
        do_backtracking(s, 0, cur_partition);
        return ans;
    }
private:
    void do_backtracking(string &s, int i, vector<string> &cur_partition) {
        if (i >= s.length()) {
            return;
        }

        for (int j = i; j < s.length() - 1; ++j) {
            int i_copy = i, j_copy = j;
            while (i_copy < j_copy && s[i_copy] == s[j_copy]) {
                ++i_copy;
                --j_copy;
            }

            if (i_copy < j_copy) {
                continue;
            }

            cur_partition.push_back(s.substr(i, j - i + 1));
            do_backtracking(s, j + 1, cur_partition);
            cur_partition.pop_back();
        }

        int i_copy = i, j_copy = s.length() - 1;
        while (i_copy < j_copy && s[i_copy] == s[j_copy]) {
            ++i_copy;
            --j_copy;
        }

        if (i_copy < j_copy) {
            return;
        }

        vector<string> found_partition = cur_partition;
        found_partition.push_back(s.substr(i, s.length() - i));
        ans.push_back(move(found_partition));
    }
private:
    vector<vector<string>> ans;
};
117018