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

from collections import defaultdict

class Solution:
    def palindromePairs(self, words: List[str]) -> List[List[int]]:
        # 1. check if the word is a palindrome
        # 2. for a given word, the last repeating character offer different variations of palindromes
        #   if there are no repeating characrters, there are only two variations.
        
        
        def is_palindrome(text):
            if text[::-1] == text:
                return True
            else:
                return False
            

        #O(N)
        word_bank = defaultdict(list)
        for i in range(len(words)):
            word_bank[words[i]].append(i)
            
        # words = sorted(words, key = lambda x: len(x), reverse=True)
        # print(words)
            
        #O(1)
        def search_palindromes(j, pp, reverse):
            nonlocal pairs
            if pp in word_bank:
                # print(j,pp)
                for pairing in word_bank[pp]:
                    if pairing != j:
                        if reverse:
                            # print('hi',(pairing,j))
                            pairs.append((pairing,j))
                        else:
                            # print('bye',(j,pairing))
                            pairs.append((j,pairing))
        
        pairs = []
        possible_palindromes = defaultdict(set)
        for i in range(len(words)):
            word = words[i]
            
            if len(word) == 0:
                for j in range(len(words)):
                    if j != i and is_palindrome(words[j]):
                        pairs.append((i,j))
                        pairs.append((j,i))
            elif len(word) == 1:
                if word in word_bank:
                    for pairing in word_bank[word]:
                        if pairing != i:
                            pairs.append((i,pairing))
                            
            elif len(word) > 1:
                for j in range(len(word)-1, -1, -1):
                    # if i == len(word):
                    #     possible_palindromes.append((word[:i] + word[:i:-1])[::-1])
                    ext = word[:j+1][::-1]
                    if is_palindrome(word + ext) and ext in word_bank and ext != word:
                        
                        possible_palindromes[ext].add((i,False))
                    # if word[j] != word[-1]:
                    #     break
                # print(possible_palindromes)
                for j in range(len(word)):
                    ext = word[j:][::-1]
                    if is_palindrome(ext + word) and ext in word_bank and ext != word:
                        # print(word, ext)
                        possible_palindromes[ext].add((i,True))
#                     if word[j] != word[0]:
#                         break
                        
                # print(word, possible_palindromes)
               
        
                # print(word_bank)
        # print(possible_palindromes)
        for ext in possible_palindromes:
            # print(ext,possible_palindromes[ext])
            for i, reverse in possible_palindromes[ext]:
                
                search_palindromes(i, ext, reverse)
        
        return list(set(pairs))
33326