<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>


// template <>
// struct std::hash<std::array<int, 2>>
// {
//     std::size_t operator()(std::array<int, 2> & a) const
//     {
//         return ((std::hash<int>()(a[0]) ^ (std::hash<int>()(a[1]) << 1)) >> 1);
//     }
// };

struct ArrayHash {
    std::size_t operator()(const std::array<int, 2>& arr) const {
        std::size_t h1 = std::hash<int>{}(arr[0]);
        std::size_t h2 = std::hash<int>{}(arr[1]);
        return h1 ^ (h2 << 1); // Combine the two hash values
    }
};

// Custom equality function (optional, since std::array provides it by default)
struct ArrayEqual {
    bool operator()(const std::array<int, 2>& arr1, const std::array<int, 2>& arr2) const {
        return arr1 == arr2;
    }
};

class Solution {
public:
    //std::unordered_map<std::array<int, 2>, bool> visited;
    std::unordered_map<std::array<int, 2>, bool, ArrayHash, ArrayEqual> visited;
    std::unordered_map<std::array<int, 2>, vector<vector<string>>, ArrayHash, ArrayEqual> partialSolutions;

    bool isPalindrome(string & s, int from, int to) {

        std::array<int, 2> indices({from, to});
            
        if(visited.find(indices) != visited.end()) {
            bool isPalindromic = visited[indices];
            return isPalindromic;
        }
            
        int i = from, j = to - 1;
        while(i < j) {
            if(s[i] != s[j]) {
                visited[indices] = false;
                return false;
            }
            i++;
            j--;
        }
        visited[indices] = true;
        return true;
    }

    void print(vector<vector<string>> sol, string comment) {
        cout << "print begin ==================" << comment << endl;
        for(auto & row : sol) {
            for(auto & word : row) {
                cout << word << " ";
            }
            cout << endl;
        }
        cout << "print end ==================" << comment << endl;
    }
    

    void complete(vector<vector<string>> & container, vector<string> & prefix, vector<vector<string>> & suffixes) {

        for(auto & subvec : suffixes) {
            vector<string> new_vec(prefix);
            new_vec.insert(new_vec.end(), subvec.begin(), subvec.end());
            container.push_back(new_vec);
        }
    }

    vector<vector<string>> partition(string & s, int from, int to) {
        vector<vector<string>> ans;
        if(from == to) {
            return ans;
        }

        for(int k = from + 1; k <= to; k++) {
            if(isPalindrome(s, from, k)) {
                string prefix = s.substr(from, k - from);
                vector<string> vec_prefix({prefix});
                if(k < to) {
                    vector<vector<string>> suffixes;
                    std::array<int, 2> indices({k, to});
                    if(partialSolutions.contains(indices)) {
                        suffixes = partialSolutions[indices];
                    } else {
                        suffixes = partition(s, k, to);
                        partialSolutions[indices] = suffixes;
                    }
                    complete(ans, vec_prefix, suffixes);
                } else {
                    ans.push_back(vec_prefix);
                }
            }
        }
        return ans;
    }

    vector<vector<string>> partition(string s) {
        vector<vector<string>> ans = partition(s, 0, s.size());
        return ans;
    }
};

// class Solution {

// private:
// bool palidrome(string &s, int start , int end)
// {
//     while(start < end)
//     {
//         if(s[start++]!=s[end--]) return false;
//     }
//     return true;
// }
// void solve(int index ,string &s , vector<vector<string>>&res , vector<string>&path)
// {
//     if(index == s.length())
//     {
//         res.push_back(path);
//         return;
//     }

//     for(int i=index ; i<s.length() ; i++)
//     {
//         if(palidrome(s,index,i))
//         {
//             path.push_back(s.substr(index , i-index+1));
//             solve(i+1,s,res,path);
//             path.pop_back();
//         }
//     }
// }
// public:
//     vector<vector<string>> partition(string s) {
//         vector<vector<string>>res;
//         vector<string>path;
//         solve(0,s,res,path);
//         return res;
//     }
// };

180743