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

    # TODO: optimize by pre-computing longest palindrome substrings centered around
    #       each index for all words; this can then be used when checking the unmatched
    #       part of both the iterated word, and the words found from the suffix-trie
    def palindromePairs(self, words: List[str]) -> List[List[int]]:

        # build suffix-trie consisting of all given words
        suffix_trie = {}
        for i, w in enumerate(words):
            node = suffix_trie
            for c in w[::-1]: node = node.setdefault(c, {})
            node["*"] = i

        res = []
        for i, w in enumerate(words):   # try each word (`w`) as the 1st part (w_i) of the palindrom-pair
            node = suffix_trie
            for k, c in enumerate(w):   # try matching all chars in current-`w` against the suffix-trie
                # check potential palindrome-pair when w_i is longer than w_j: e.g. ("a", "") & ("lls", "ll")
                if "*" in node and i != node["*"] and w[k:] == w[k:][::-1]: res.append((i, node["*"]))
                
                if c in node: node = node[c]
                else: break
            else:                       # the entire current-`w` found as a prefix in the suffix-trie
                for j in Solution.get_unmatched_palin_substring_idxs(node):
                    if i != j: res.append((i, j))

        return res


    # finds and returns all word-indices of unmatched remaining substrings that are also palindromes
    @staticmethod
    def get_unmatched_palin_substring_idxs(trie_node):
        ss_idxs = {}
        def _fetch(node, ss):            
            for c, nn in node.items():
                if c == "*": ss_idxs[ss] = node[c]
                else: _fetch(nn, f"{ss}{c}")
        _fetch(trie_node, "")
        return [idx for (ss, idx) in ss_idxs.items() if ss == ss[::-1]]
40961