<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:
    def palindromePairs(self, words: List[str]) -> List[List[int]]:
        ans = []
        trie = Trie(words)
        for i, word in enumerate(words):
            trie.insert(word, i)
        for i in range(len(words)):
            candidates = trie.search(i)
            print(i, candidates)
            for candidate in candidates:
                ans.append([candidate, i])
        return ans
        

class Trie:
    def __init__(self, words):
        self.root = TrieNode()
        self.words = words

    def insert(self, word, index=-1):
        node = self.root
        for c in word:
            if c not in node.children:
                node.children[c] = TrieNode()
            node = node.children[c]
        node.index = index

    def isPalindrome(self, word, i, j):
        if j < 0:
            j = len(word) + j
        while i < j:
            if word[i] != word[j]:
                return False
            i += 1
            j -= 1
        return True

    def search(self, wordIndex):
        word = self.words[wordIndex]
        if not word:
            return [i for i in range(len(self.words)) if i != wordIndex and self.isPair(i, wordIndex)]
        node = self.root
        i = -1
        candidates = []
        while i >= -len(word):
            c = word[i]
            if c not in node.children and node.index == -1:
                break
            if node.index >= 0:
                candidates.append(node.index)
            if c in node.children:
                node = node.children[c]
                i -= 1
            else:
                break
        if i<-len(word):
            candidates += node.getRest()
        return [candidate for candidate in candidates if self.isPair(candidate, wordIndex)]

    def isPair(self, candidate, wordIndex):
        if candidate==wordIndex:
            return False
        word1 = self.words[candidate]
        word2 = self.words[wordIndex]
        if len(word1)>len(word2):
            return self.isPalindrome(word1, len(word2), -1)
        elif len(word1)<len(word2):
            return self.isPalindrome(word2, 0, -len(word1)-1)
        else:
            return True


class TrieNode:
    def __init__(self):
        self.children = {}
        self.index = -1

    def getRest(self):
        def helper(node, ans):
            if node.index >= 0:
                ans.append(node.index)
            for child in node.children.values():
                helper(child, ans)
        ans = []
        helper(self, ans)
        return ans

63866