<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 TrieNode:
    def __init__(self):
        self.children = dict()
        self.end = False
        self.idx = -1
        self.palindromeIdxs = list()

class Solution:
    def __init__(self):
        self.root = TrieNode()
        
    def palindromePairs(self, words: List[str]) -> List[List[int]]:
        res = list()
        
        # populate the trie with
        # the reverse of every word.
        # once we're done inserting
        # we're going to have 3 conditions
        for i in range(len(words)):
            cur = self.root
            rWord = words[i][::-1]
            for j in range(len(rWord)):
                # if the current word (from j onwards)
                # is a palindrome, add it's index to the trie node
                # (palindromIdx list) we'll use it later on to find combinations
                if self.isPalindrome(rWord[j:]):
                    cur.palindromeIdxs.append(i)
                    
                if rWord[j] not in cur.children:
                    cur.children[rWord[j]] = TrieNode()
                cur = cur.children[rWord[j]]
                
            # once the word is done
            # add it's index to the trie node
            cur.end = True
            cur.idx = i
            
        for i in range(len(words)):
            self.search(words[i], i, res)
            
        return res
        
    # to find all pairse, we can have
    # conditions:
    # 1. exact match (abc, cba)
    # 2. long word, short word in trie match (abbcc, a)
    # 3. short word, long word in trie match (lls, sssll)
    def search(self, word, idx, res):   
        cur = self.root
        for i in range(len(word)):
            # 2. long word, short trie
            # so the trie ended here and 
            # we have matched till the ith
            # character, so we check if the
            # remaining of the word is also a
            # palindrome, if yes, then we have a pair
            # for e.g. word = abcdaa, trieWord = bcda
            # we can make a pair like abcdaabcda
            if cur.end and self.isPalindrome(word[i:]):
                res.append([idx, cur.idx])
                
            if word[i] not in cur.children:
                return
            cur = cur.children[word[i]]        
        
        # 1. exact match
        # in the given list, for that 
        # we'll take every word and then
        # check if the reverse of that
        # word lies in the trie
        # for e.g. for abc and cba
        # the trie would have both c->b->a and a->b->c
        # but when we take the first word (abc)
        # we'll match this with a->b->c which is
        # actually cba and so we found a match
        if cur.end and cur.idx != idx:
            res.append([cur.idx, idx])
        
        # 3. long trie, short word
        # so the trie still has items (not cur.end)
        # and the word has ended, it's the exact
        # opposite of point 2
        # for e.g. word=abcd trieWord=bcdaa
        # we can have a pair bcdaaabcd
        # and so we have a pair
        for pIdx in cur.palindromeIdxs:
            res.append([idx, pIdx])
                
        return
        
        
    def isPalindrome(self, s):
        return s == s[::-1]

212748