<p>You are given a <strong>0-indexed</strong> array of <strong>unique</strong> strings <code>words</code>.</p>

<p>A <strong>palindrome pair</strong> is a pair of integers <code>(i, j)</code> such that:</p>

<ul>
	<li><code>0 &lt;= i, j &lt; words.length</code>,</li>
	<li><code>i != j</code>, and</li>
	<li><code>words[i] + words[j]</code> (the concatenation of the two strings) is a <span data-keyword="palindrome-string">palindrome</span>.</li>
</ul>

<p>Return <em>an array of all the <strong>palindrome pairs</strong> of </em><code>words</code>.</p>

<p>You must write an algorithm with&nbsp;<code>O(sum of words[i].length)</code>&nbsp;runtime complexity.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;abcd&quot;,&quot;dcba&quot;,&quot;lls&quot;,&quot;s&quot;,&quot;sssll&quot;]
<strong>Output:</strong> [[0,1],[1,0],[3,2],[2,4]]
<strong>Explanation:</strong> The palindromes are [&quot;abcddcba&quot;,&quot;dcbaabcd&quot;,&quot;slls&quot;,&quot;llssssll&quot;]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;bat&quot;,&quot;tab&quot;,&quot;cat&quot;]
<strong>Output:</strong> [[0,1],[1,0]]
<strong>Explanation:</strong> The palindromes are [&quot;battab&quot;,&quot;tabbat&quot;]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;a&quot;,&quot;&quot;]
<strong>Output:</strong> [[0,1],[1,0]]
<strong>Explanation:</strong> The palindromes are [&quot;a&quot;,&quot;a&quot;]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 5000</code></li>
	<li><code>0 &lt;= words[i].length &lt;= 300</code></li>
	<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>

class Solution {
public:

    const int P = 29;
    const long long MOD = 1000000007;

    vector<vector<int>> palindromePairs(vector<string>& words) {
        vector<long long> pw(301, 0);
        pw[0] = 1;
        for (int i = 1; i <= 300; ++i) {
            pw[i] = 1ll * pw[i - 1] * P % MOD;
        }
        vector<vector<int> > isPref, isSuff;
        vector<vector<int> > hashPref, hashSuff;
        for (const auto& s : words) {
            vector<long long> pref(s.size() + 2, 0), suff(s.size() + 2, 0);
            pref[0] = 0;
            for (int i = 1; i <= s.size(); ++i) {
                int code = s[i - 1] - 'a' + 1;
                pref[i] = pref[i - 1] + 1ll * pw[i - 1] * code % MOD;
                pref[i] %= MOD;
            }
            suff[s.size() + 1] = 0;
            for (int i = (int)s.size(); i >= 1; --i) {
                int code = s[i - 1] - 'a' + 1;
                suff[i] = suff[i + 1] + 1ll * pw[(int)s.size() - i] * code % MOD;
                suff[i] %= MOD;
            }
            vector<int> prefAdd, suffAdd;
            for (int i = 1; i <= s.size(); ++i) {
                long long hash1 = pref[i], hash2 = suff[1] - suff[i + 1];
                if (hash2 < 0) hash2 += MOD;
                int len = (int)s.size() - i;
                hash1 = 1ll * hash1 * pw[len] % MOD;
                if (hash1 == hash2) prefAdd.push_back(1); else prefAdd.push_back(0);
            }
            for (int i = s.size(); i >= 1; --i) {
                long long hash1 = suff[i], hash2 = pref[s.size()] - pref[i - 1];
                if (hash2 < 0) hash2 += MOD;
                int len = i - 1;
                hash1 = 1ll * hash1 * pw[len] % MOD;
                if (hash1 == hash2) suffAdd.push_back(1); else suffAdd.push_back(0);
            }
            reverse(suffAdd.begin(), suffAdd.end());
            isPref.push_back(prefAdd);
            isSuff.push_back(suffAdd);
        }

        for (int i = 0; i < words.size(); ++i) {
            vector<int> curHash;
            int cur = 0;
            for (int j = 0; j < words[i].size(); ++j) {
                int code = words[i][j] - 'a' + 1;
                cur = 1ll * cur + 1ll * pw[j] * code % MOD;
                if (cur >= MOD) cur -= MOD;
                curHash.push_back(cur);
            }
            hashPref.push_back(curHash);
            curHash.clear();
            cur = 0;
            for (int j = (int)words[i].size() - 1; j >= 0; --j) {
                int code = words[i][j] - 'a' + 1;
                int len = (int)words[i].size() - j - 1;
                cur = 1ll * cur + 1ll * pw[len] * code % MOD;
                if (cur >= MOD) cur -= MOD;
                curHash.push_back(cur);
            }
            reverse(curHash.begin(), curHash.end());
            hashSuff.push_back(curHash);
        }

        vector<vector<int> > ans;
        int n = words.size();
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i == j) continue;
                int l1 = words[i].size(), l2 = words[j].size();
                if (words[i].empty() && words[j].empty()) {
                    ans.push_back({i, j});
                    continue;
                }
                if (words[i].empty()) {
                    if (isPref[j][l2 - 1]) ans.push_back({i, j});
                    continue;
                }
                if (words[j].empty()) {
                    if (isPref[i][l1 - 1]) ans.push_back({i, j});
                    continue;
                }
                if (words[i].size() < words[j].size()) {
                    if (hashPref[i][l1 - 1] != hashSuff[j][l2 - l1]) continue;
                    if (isPref[j][l2 - l1 - 1]) ans.push_back({i, j});
                } else if (words[i].size() > words[j].size()) {
                    if (hashPref[i][l2 - 1] != hashSuff[j][0]) continue;
                    if (isSuff[i][l2]) ans.push_back({i, j});
                } else {
                    if (hashPref[i][l1 - 1] == hashSuff[j][0]) {
                        ans.push_back({i, j});
                    }
                }
            }
        }
        return ans;
    }
};
394155