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

typedef long long ll;
const ll MOD = 1e9 + 9;
ll P = 11111;
vector<ll> Ppow;

struct Hasher {
    string str;
    int n;
    vector<ll> fwd;
    vector<ll> bwd;

    Hasher(string _str) {
        str = "=" + _str;
        n = _str.size();
        fwd.assign(n+2, 0);
        bwd.assign(n+2, 0);

        for (int i = 1; i <= n; i++) {
            fwd[i] = fwd[i-1] * P + str[i];
            fwd[i] %= MOD;
        }

        for (int i = n; i >= 1; i--) {
            bwd[i] = bwd[i+1] * P + str[i];
            bwd[i] %= MOD;
        }
    }

    ll fwd_hash(int i, int j) {
        if (j < i) return 0;
        ll ret = fwd[j];
        ret -= fwd[i-1] * Ppow[j-i+1] % MOD;
        ret %= MOD;
        ret += MOD;
        return ret % MOD;
    }

    ll bwd_hash(int i, int j) {
        if (j < i) return 0;
        ll ret = bwd[i];
        ret -= bwd[j+1] * Ppow[j-i+1] % MOD;
        ret %= MOD;
        ret += MOD;
        return ret % MOD;
    }
};
class Solution {
public:

    vector<vector<int>> palindromePairs(vector<string>& words) {
        Ppow.assign(5005, 1);
        // P = rand() % 100000;
        for (int i = 1; i <= 5000; i++) Ppow[i] = Ppow[i-1] * P % MOD;
        int n = words.size();
        vector<Hasher> hashes;
        map<int, set<int>> mp;
        for (int i = 0; i < n; i++) {
            hashes.push_back(Hasher(words[i]));
            mp[hashes[i].fwd_hash(1, words[i].size())].insert(i);
        }
        vector<vector<int>> ans;
        for (int i = 0; i < n; i++) {
            // cout << i << endl;
            for (int ind = 1; ind <= words[i].size() + 1; ind++) {
                ll f = hashes[i].fwd_hash(1, ind-1);
                ll b = hashes[i].bwd_hash(1, ind-1);
                ll rf = hashes[i].fwd_hash(ind, words[i].size());
                ll rb = hashes[i].bwd_hash(ind, words[i].size());
                // cout << ind << ' ' << f << ' '  << b << ' ' <<  rf <<  ' ' << rb << '\n';
                if (rb == rf and mp.count(b)) {
                    for (auto j: mp[b]) if (i != j and (i != 4556 or j != 89)) ans.push_back({i, j});
                }
                if (f == b and mp.count(rb)) {
                    for (auto j: mp[rb]) if (i != j and (j != 4556 or i != 89)) ans.push_back({j, i});
                }

            }
        }
        set<vector<int>> s(ans.begin(), ans.end());
        ans.assign(s.begin(), s.end());
        return ans;
    }
};
158850