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

#define ll long long int
#define ii pair<ll, ll>
#define mod 1000000007
class Solution {
public:
    vector<ll> pw37, pw41;
    void generateHash(string word, vector<ll> &fwd37, vector<ll> &fwd41, vector<ll> &bck37, vector<ll> &bck41) {
        int n = (int)word.size();
        fwd37.resize(n); fwd41.resize(n); bck37.resize(n); bck41.resize(n);
        for(int i = 0; i < n; i++) {
            if(i == 0) {
                fwd37[i] = fwd41[i] = word[i];
            } else {
                fwd37[i] = (fwd37[i - 1] * 37 + word[i]) % mod;
                fwd41[i] = (fwd41[i - 1] * 41 + word[i]) % mod;
            }
        }
        for(int i = n - 1; i >= 0; i--) {
            if(i == n - 1) {
                bck37[i] = bck41[i] = word[i];
            } else {
                bck37[i] = (bck37[i + 1] * 37 + word[i]) % mod;
                bck41[i] = (bck41[i + 1] * 41 + word[i]) % mod;
            }
        }
    }
    ii getFwd(int l, int r, vector<ll> &fwd37, vector<ll> &fwd41) {
        if(r < l) return ii(0, 0);
        long long f37, f41;
        f37 = fwd37[r]; 
        f41 = fwd41[r];
        if(l > 0) {
            f37 -= (fwd37[l - 1] * pw37[r - l + 1]) % mod;
            f41 -= (fwd41[l - 1] * pw41[r - l + 1]) % mod;
        }
        if(f37 < 0) f37 += mod;
        if(f41 < 0) f41 += mod;
        return ii(f37, f41);
    }
    ii getBck(int l, int r, vector<ll> &bck37, vector<ll> &bck41) {
        if(r < l) return ii(0, 0);
        int n = (int)bck37.size();
        long long b37, b41;
        b37 = bck37[l]; 
        b41 = bck41[l];
        if(r < n - 1) {
            b37 -= (bck37[r + 1] * pw37[r - l + 1]) % mod;
            b41 -= (bck41[r + 1] * pw41[r - l + 1]) % mod;
        }
        if(b37 < 0) b37 += mod;
        if(b41 < 0) b41 += mod;
        return ii(b37, b41);
    }
    bool check(ii a, ii b) {
        if(a.first == b.first and a.second == b.second) return true;
        return false;
    }
    bool checkPal(int l, int r, vector<ll> &fwd37, vector<ll> &fwd41, vector<ll> &bck37, vector<ll> &bck41) {
        return check(getFwd(l, r, fwd37, fwd41), getBck(l, r, bck37, bck41));
    }
    vector<vector<int>> palindromePairs(vector<string>& words) {
        int n = (int)words.size();
        //long long int mod1 = 1000000007, mod2 = 1000000009;
        vector<vector<long long int>> fwd37(n), fwd41(n), bck37(n), bck41(n);
        pw37.push_back(1); pw41.push_back(1);
        for(int i = 0; i < n; i++) {
            generateHash(words[i], fwd37[i], fwd41[i], bck37[i], bck41[i]);
        }
        for(int i = 1; i <= 300; i++) { 
            pw37.push_back((pw37[i - 1] * 37) % mod);
            pw41.push_back((pw41[i - 1] * 41) % mod);
        }
        vector<vector<int>> ret;
        //return ret;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(i == j) continue;
                int l = (int)words[i].size(), r = (int)words[j].size();
                if(l > r) {
                    ii x = getFwd(0, r - 1, fwd37[i], fwd41[i]);
                    ii y = getBck(0, r - 1, bck37[j], bck41[j]);
                    if(!check(x, y)) continue;
                    if(!checkPal(r, l - 1, fwd37[i], fwd41[i], bck37[i], bck41[i])) continue;
                    vector<int> curr; curr.push_back(i); curr.push_back(j);
                    ret.push_back(curr);
                } else if(l < r) {
                    bool flag = true;
                    ii x = getFwd(0, l - 1, fwd37[i], fwd41[i]);
                    ii y = getBck(r - l, r - 1, bck37[j], bck41[j]);
                    if(!check(x, y)) continue;
                    if(!checkPal(0, r - l - 1, fwd37[j], fwd41[j], bck37[j], bck41[j])) continue;
                    vector<int> curr; curr.push_back(i); curr.push_back(j);
                    ret.push_back(curr);            
                } else if(l == r) {
                    ii x = getFwd(0, r - 1, fwd37[i], fwd41[i]);
                    ii y = getBck(0, r - 1, bck37[j], bck41[j]);
                    if(!check(x, y)) continue;
                    vector<int> curr; curr.push_back(i); curr.push_back(j);
                    ret.push_back(curr);
                }
            }
        }
        return ret;
    }
};
192465