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

struct Trie {
    bool end;
    int ix;
    Trie* childs[26] = {};
    Trie(bool end, int ix) {
        this->end = end;
        this->ix = ix;
    }
};

class Solution {
public:
    void addWord(Trie* root, string& word, int ix, int cur) {
        if (cur >= word.size()) return ;
        Trie* next;
        if (root->childs[word[cur] - 'a'] == NULL) {
            next = new Trie(cur == word.size() - 1, ix);
        }
        else {
            next = root->childs[word[cur] - 'a'];
        }

        if (cur == word.size() - 1) { 
            next->end = true;
            next->ix = ix;
            root->childs[word[cur] - 'a'] = next;
            return ;
        }
        root->childs[word[cur] - 'a'] = next;
        addWord(next, word, ix, cur + 1);
    }

    int search(Trie* root, string& word, int cur, int end) {
        // cout << "Search: " << word << " " << cur << " " << end << endl;
        if (cur > end) return -1;
        if (cur == end) {
            if (root->childs[word[cur] - 'a'] && root->childs[word[cur] - 'a']->end) {
                return root->childs[word[cur] - 'a']->ix;
            }

            return -1;
        }

        Trie* next;
        // cout << word[cur] << endl;
        // cout << word[cur] - 'a' << endl;
        if (root->childs[word[cur] - 'a'] == NULL) {
            return -1;
        }
        else {
            next = root->childs[word[cur] - 'a'];
        }

        return search(next, word, cur + 1, end);
    }

    bool isPalindrome(string& word, int start, int end) {
        for (int i = start; i <= (start + end) / 2; i++) {
            if (word[i] != word[end - (i - start)]) return false;
        }

        return true;
    }

    vector<vector<int>> palindromePairs(vector<string>& words) {
        // insert everything into trie
        // store a vector of indexes that has that word for each node in the trie
        

        // loop through each word, there are two cases
        // 1. the palindrome is of even length, then find the reverse in the trie
        // 2. the palindrom is of odd length, so then we have 2 subcases
        //    a. the first word is the longer word
        //       i. 

        Trie* root = new Trie(false, -1);
        set<pair<int, int>> ans;

        int emptyIx = -1;
        for (int i = 0; i < words.size(); i++) {
            if (words[i].size() == 0) emptyIx = i;
            addWord(root, words[i], i, 0);
        }

        for (int i = 0; i < words.size(); i++) {
            // two cases:
            // left + mid
            string rev = words[i];
            reverse(rev.begin(), rev.end());
            int fd = search(root, rev, 0, rev.size() - 1);
            if (fd == i && emptyIx != -1) {
                ans.insert({i, emptyIx});
                ans.insert({emptyIx, i});
            }
            else if (fd != -1 && fd != i) ans.insert({i, fd});
            for (int j = 0; j < words[i].size(); j++) {
                if (isPalindrome(words[i], j, words[i].size() - 1)) {
                    // cout << words[i] << " " << rev << " " << j << endl;
                    int fd = search(root, rev, rev.size() - 1 - j + 1, rev.size() - 1);
                    if (fd != -1) ans.insert({i, fd});
                }
            }

            // mid + right

            for (int j = 0; j < words[i].size(); j++) {
                if (isPalindrome(words[i], 0, j)) {
                    int fd = search(root, rev, 0, rev.size() - 1 - j - 1);
                    if (fd != -1) ans.insert({fd, i});
                }
            }
        }

        vector<vector<int>> res;

        for (auto x : ans) {
            res.push_back({x.first, x.second});
        }

        return res;
    }
};
570634