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

    template<int K>
    struct Vertex {
    
        int next[K];
        vector<int> entries;
        vector<int> ends;
    
        Vertex() {
    
            memset(next, 0, sizeof next);
        }
    
        int& operator[](int id){
    
            return next[id];
        }
    };

    template<int K>
    struct Trie {

        vector<Vertex<K>> v;
    
        Trie() {
            
            v.resize(1);
        }
    
        void insert(const string& s, int index, const vector<int>& good_indices) {
    
            int current = 0;
            int cur_index = 0;
            int arr_index = 0;
            const int len = s.size();
            for (auto &c : s) {
    
                if (!v[current][c - 'a']) {
    
                    v[current][c - 'a'] = v.size();
                    v.emplace_back(Vertex<K>());
                }
    
                current = v[current][c - 'a'];
                ++cur_index;

                if (arr_index < (int)good_indices.size() && 
                    len - cur_index == good_indices[arr_index]) {

                    v[current].entries.push_back(index);
                    ++arr_index;
                }

            }
            v[current].ends.push_back(index);
        }

        int search(const string& s, int index, const vector<int>& good_indices, vector<vector<int>>& ans) {

            int current = 0;
            int cur_index = 0;
            int arr_index = 0;
            int len = s.size();
            for (auto c : s) {

                if (!v[current][c - 'a']) return -1;
                current = v[current][c - 'a'];

                ++cur_index;
                if (arr_index < (int)good_indices.size() && 
                    len - cur_index == good_indices[arr_index]) {

                    for (auto i : v[current].ends) {

                        if (i != index) ans.push_back( { index, i } );
                    }
                    ++arr_index;
                }
            }

            return current;
        }
    };

    pair<vector<int>, bool> palindrome_indices(const string& s, int original_length) {

        const int n = s.size();
        vector<int> z(n);

        int l = 0;
        int r = 0;

        for (int i = 1; i < n; ++i) {

            if (i < r) {

                z[i] = min(r - i, z[i - l]);
            }

            while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
            if (i + z[i] > r) {

                l = i;
                r = z[i];
            }
        }

        vector<int> ans;

        // not the whole string !
        for (int i = original_length + 2; i < n; ++i) {

            if (z[i] == n - i) ans.push_back(z[i]);
        }

        return { ans, z[original_length + 1] == original_length } ;
    }

public:
    vector<vector<int>> palindromePairs(vector<string>& words) {
        
        auto trie = Trie<26>();

        int index = 0;
        vector<int> palis;
        for (auto &s : words) {

            if (s.empty()) {

                ++index;
                continue;
            }

            string reversed_s = s;
            reverse(reversed_s.begin(), reversed_s.end());
            auto [z_array, is_pali] = palindrome_indices(s + "#" + reversed_s, s.size());
            if (is_pali) palis.push_back(index);
            // I don't care if the string itself is a palindrome or not ?
            trie.insert(reversed_s, index, z_array);
            ++index;
        }

        vector<vector<int>> ans;
        index = 0;
        for (auto &s : words) {

            if (s.empty()) {

                for (auto i : palis) {

                    ans.push_back( { i, index});
                    ans.push_back( { index, i } );
                }

                ++index;
                continue;
            }

            string reversed_s = s;
            reverse(reversed_s.begin(), reversed_s.end());
            auto [z_array, pp] = palindrome_indices(reversed_s + "#" + s, s.size());

            int trie_index = trie.search(s, index, z_array, ans);
            if (trie_index != -1) {

                for (auto i : trie.v[trie_index].entries) {

                    if (i != index) ans.push_back( { index, i } );
                }

                for (auto i : trie.v[trie_index].ends) {

                    if (i != index) ans.push_back( { index, i } );
                }
            }

            ++index;
        }

        return ans;
    }
};
545423