<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 
{
public:
    class TrieNode 
    {
    public:
        TrieNode* children[26];
        int16_t wordIndex;

        TrieNode() : wordIndex(-1)
        {
            std::fill(std::begin(children), std::end(children), nullptr);
        }

        ~TrieNode() 
        {            
            std::for_each(std::begin(children), std::end(children),
                          [](TrieNode* childPtr) 
                          {
                              delete childPtr;
                              childPtr = nullptr;
                          });
        }

        void insert(TrieNode* rootNode, const std::string& vi, int16_t _wordIndex) 
        {
            TrieNode* current = rootNode;

            for (int16_t i = vi.size() - 1; i >= 0; --i) 
            {
                int16_t childIndex = vi[i] - 'a';
                if (current->children[childIndex] == nullptr) 
                {
                    current->children[childIndex] = new TrieNode();
                }
                current = current->children[childIndex];
            }

            current->wordIndex = _wordIndex;
        }

        bool searchWithIndices(TrieNode* rootNode, const std::string& vi, int16_t start, int16_t end, int16_t& outWordIndex)
        {
            TrieNode* current = rootNode;

            for (int16_t i = start; i < end; ++i) 
            {
                int16_t childIndex = vi[i] - 'a';
                if (current->children[childIndex] == nullptr) 
                {
                    return false;
                }
                current = current->children[childIndex];
            }

            if (current->wordIndex != -1) 
            {
                outWordIndex = current->wordIndex;
                return true;
            }

            return false;
        }

        bool isPalindrome(const std::string& str, int16_t i, int16_t j) 
        {
            while (i < j) 
            {
                if (str[i++] != str[j--])
                {
                    return false;
                }
            }
            return true;
        }
    };

    vector<vector<int>> palindromePairs(vector<string>& words) 
    {
        vector<vector<int>> outputVector;
        outputVector.reserve(words.size());

        TrieNode* rootNode = new TrieNode();

        for (uint16_t i = 0; i < words.size(); ++i) 
        {
            rootNode->insert(rootNode, words[i], i);
        }

        string currentWord;
        int16_t foundIndex;
        uint16_t len;

        for(uint16_t i = 0; i < words.size(); i++) 
        {
            currentWord = words[i];
            len = currentWord.size();

            for(uint16_t j = 0; j <= len; j++) 
            {
                // Check if the suffix (from j to len - 1) is a palindrome
                if(rootNode->isPalindrome(currentWord, j, len - 1)) 
                {
                    // Search for the reverse of the prefix (from 0 to j)
                    if(rootNode->searchWithIndices(rootNode, currentWord, 0, j, foundIndex) && foundIndex != i) 
                    {
                        outputVector.push_back({i, foundIndex});
                    }
                }

                // Check if the prefix (from 0 to j - 1) is a palindrome
                if(j > 0 && rootNode->isPalindrome(currentWord, 0, j - 1)) 
                {
                    // Search for the reverse of the suffix (from j to len)
                    if(rootNode->searchWithIndices(rootNode, currentWord, j, len, foundIndex) && foundIndex != i) 
                    {
                        outputVector.push_back({foundIndex, i});
                    }
                }
            }
        }

        delete rootNode;

        return outputVector;
    }
};

579038