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

vector<string> a;
struct Trie{
    map<int, Trie*> child;
    vector<int> belongTo;
    int end = -1;
    Trie(){}
    
    void insert(string s, int idx){
        Trie *root = this;
        root -> belongTo.push_back(idx);
        for(int i = 0; i < s.size(); i ++){
            int ind = s[i] - 'a';
            if(!(root -> child).count(ind)){
                root -> child[ind] = new Trie();
            }
            root = root -> child[ind];
            root -> belongTo.push_back(idx);
        }
        root -> end = idx;
    }
    
    bool isPalindrome(string s){
        int i = 0, j = s.size() - 1;
        while(i < j){
            if(s[i] != s[j]) return false;
            i++;
            j--;
        }
        return true;
    }
    
    set<int> getSuffix(string s, int currIdx) {
        set<int> res;
        Trie *root = this;
        // Case - 1  
        for(int i = 0; i < s.size(); i ++){
            int ind = s[i] - 'a';
            if(root -> end != -1 && isPalindrome(s.substr(i))){
                res.insert(root -> end);
            }
            if((root -> child).count(ind)){
                root = root -> child[ind];
            }else{
                return res;
            }
        }

        // Case - 2 
        if(root -> end != -1) {
            res.insert(root -> end);
        }
            
        // Case - 3
        vector<int> wordidx = root -> belongTo;
        for(int idx : wordidx){
            if(currIdx == idx) continue;
            string ss = a[idx];
            reverse(ss.begin(), ss.end());
            ss = ss.substr(s.size());
            if(isPalindrome(ss)){
                res.insert(idx);
            }
        }
        return res;
    }
};


// debbedcba, abc
// abcdebbed
// (5000 * (300 + 300))
class Solution {
public:
    vector<vector<int>> palindromePairs(vector<string>& A){
        a = A;
        
        Trie *root = new Trie();
        for(int i = 0; i < a.size(); i++){
            string s = a[i];
            reverse(s.begin(), s.end());
            root -> insert(s, i);
        }
        
        set<vector<int>> st;
        for(int i = 0; i < a.size(); i++){
            string s = a[i];
            set<int> ans = root -> getSuffix(s, i);
            for(auto v: ans){
                if(v != i) st.insert({i, v});
            }
        }
        return vector<vector<int>>(st.begin(), st.end());
    }
};
478193