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

struct Node {
    Node* links[26];
    vector<int> restIsPalindrome;
    int flag = -1;
    bool containsKey(char ch) { return (links[ch - 'a'] != NULL); }
    Node* get(char ch) { return links[ch - 'a']; }
    void put(char ch, Node* node) { links[ch - 'a'] = node; }
    void setpalin(int i) { restIsPalindrome.push_back(i); }
    void setend(int idx) { flag = idx; }
};
class Trie {
public:
    Node* root;
    Trie() { root = new Node(); }
    void insert(string& word, int idx) {
        int n = word.size();
        Node* node = root;
        for (int i = n - 1; i >= 0; i--) {
            if (isPalindrome(word, 0, i)) {
                node->setpalin(idx);
            }
            if (!node->containsKey(word[i])) {
                node->put(word[i], new Node());
            }
            node = node->get(word[i]);
        }
        node->setend(idx);
        return;
    }
    void search(string& word, int idx, vector<vector<int>>& res) {
        Node* node = root;
        int n = word.size();
        for (int i = 0; i < n; i++) {
            if (node->flag != -1 &&
                isPalindrome(
                    word, i,
                    n - 1)) // one string is completed in trie and the rest of
                            // current word is palindrome eg - s and sll
            {
                res.push_back({idx, node->flag});
            }
            if (!node->containsKey(word[i]))
                return;
            node = node->get(word[i]);
        }
        // eg abcd and dcba / aaaa
        if (node->flag != -1 & node->flag != idx) {
            res.push_back({idx, node->flag});
        }
        for (int j : node->restIsPalindrome) {
            res.push_back({idx, j});
        }
    }
    bool isPalindrome(string& s, int left, int right) {
        while (left <= right) {
            if (s[left++] != s[right--]) {
                return false;
            }
        }
        return true;
    }
};
class Solution {
public:
    vector<vector<int>> palindromePairs(vector<string>& words) {
        int n = words.size();
        Trie trie;
        for (int i = 0; i < n; i++) {
            trie.insert(words[i], i);
        }
        vector<vector<int>> ans;
        for (int i = 0; i < words.size(); i++) {
            trie.search(words[i], i, ans);
        }
        return ans;
    }
};
595845