<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>> res;
    unordered_set<string> palindromes;
    bool palindrome(string &word) {
        if (word.size() == 0) return false;
        if (palindromes.find(word) != palindromes.end()) return true;
        int n = word.size();
        for (int i = 0; i < n/2; i++) {
            if (word[i] != word[n-i-1]) return false;
        }
        palindromes.insert(word);
        return true;
    }
    void dfs(string &s, string &word, vector<string> &path, int pos) {
        if (pos == s.size()) {
            int count = 0;
            for (auto str : path) {
                count += str.size();
            }
            if (count == s.size()) res.push_back(path);
            return;
        }
        word.push_back(s[pos]);
        if (palindrome(word)) {
            path.push_back(word);
            string temp = "";
            dfs(s, temp, path, pos+1);
            path.pop_back();
        }

        dfs(s, word, path, pos+1);
        return;
    }
    vector<vector<string>> partition(string s) {
        string word = "";
        vector<string> path;
        dfs(s, word, path, 0);
        return res;
    }
};

97900