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

#include <map>
#include <string>
#include <vector>
#include <algorithm>  // for reverse
#include <iostream>
#include <set>

using namespace std;

class TrieNode {
public:
    map<char, TrieNode*> children;
    int endingWordIndex;
    set<int> wordsEndingAsPalindrome;  // Use a set to avoid duplicates

    TrieNode() : endingWordIndex(-1) {}

    ~TrieNode() {
        // Recursive destructor to clean up children
        for (auto& child : children) {
            delete child.second;
        }
    }

    void setEndingWordIndex(int index) {
        this->endingWordIndex = index;
    }

    bool isChildExist(char c) {
        return this->children.find(c) != this->children.end();
    }

    TrieNode* getChild(char c) {
        auto it = this->children.find(c);
        return (it != this->children.end()) ? it->second : nullptr;
    }

    void addChild(char c) {
        this->children[c] = new TrieNode();
    }

    void addPalindromeIndex(int index) {
        wordsEndingAsPalindrome.insert(index);  // Insert into a set
    }
};

bool isPalindrome(int start, const string& s) {
    int l = start;
    int r = s.size() - 1;
    while (l < r) {
        if (s[l] != s[r])
            return false;
        l++;
        r--;
    }
    return true;
}

class Trie {
    TrieNode* root;

public:
    Trie() : root(new TrieNode()) {}

    ~Trie() {
        delete root;
    }

    void add(const string& s, int index) {
        if (s.empty()) return;  // Skip adding empty string

        TrieNode* r = root;
        int pos = 0;
        for (char c : s) {
            if (!r->isChildExist(c)) {
                r->addChild(c);
            }
            r = r->getChild(c);
            if (pos + 1 < s.size() && isPalindrome(pos + 1, s)) {
                r->addPalindromeIndex(index);
            }
            pos++;
        }
        r->setEndingWordIndex(index);
    }

    vector<vector<int>> get(const string& s, int index) {
        if (s.empty()) return {};  // No pairs can be found if s is empty

        TrieNode* r = root;
        vector<vector<int>> positions;
        for (int i = 0; i < s.size(); i++) {
            char c = s[i];
            if (!r->isChildExist(c)) {
                return positions;
            }

            r = r->getChild(c);
           

            if (r->endingWordIndex != -1 && r->endingWordIndex != index &&
                isPalindrome(i + 1, s)) {
                positions.push_back({index, r->endingWordIndex});
            }
        }

        for (int pos : r->wordsEndingAsPalindrome) {
            if (pos != index)
                positions.push_back({index, pos});
        }
        return positions;
    }
};

class Solution {

public:
    vector<vector<int>> palindromePairs(vector<string>& words) {
        Trie trie;
        vector<vector<int>> res;

        // Handle empty string separately
        int emptyStringIndex = -1;
        for (int i = 0; i < words.size(); i++) {
            if (words[i].empty()) {
                emptyStringIndex = i;
            } else {
                string curr = words[i];
                reverse(curr.begin(), curr.end());
                trie.add(curr, i);
            }
        }

        for (int i = 0; i < words.size(); i++) {
            if (i == emptyStringIndex) continue;  // Skip the empty string for now

            string curr = words[i];
            for (auto item : trie.get(curr, i)) {
                res.push_back(item);
            }
        }

        // If the empty string is present, add pairs with it
        if (emptyStringIndex != -1) {
            for (int i = 0; i < words.size(); i++) {
                if (i == emptyStringIndex) continue;

                string curr = words[i];
                if (isPalindrome(0, curr)) {
                    res.push_back({emptyStringIndex, i});
                    res.push_back({i, emptyStringIndex});
                }
            }
        }

        return res;
    }
};

553826