<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 {
    struct Trie
    {
      std::vector<std::vector<int>> trie;
      std::vector<int> index;
      
      Trie()
      {
        trie.push_back(std::vector<int>(26));
        index.push_back(-1);
      }
      
      void insert(const std::string& str, int idx)
      {
        int node = 0;
        for(char c: str)
        {
          if(trie[node][c-'a']==0)
          {
            trie[node][c-'a'] = trie.size();
            trie.push_back(std::vector<int>(26));
            index.push_back(-1);
          }
          node = trie[node][c-'a'];
        }
        index[node] = idx;
      }
      
      bool is_palindrome(const std::string& str, int lo, int hi)
      {
        while(lo<hi)
        {
          if(str[lo]!=str[hi])
            return false;
          ++lo;
          --hi;
        }
        return true;
      }
      
      void dfs(int node, std::vector<int>& result, std::string& current)
      {
        if(index[node]!=-1 && is_palindrome(current, 0, int(current.size())-1))
        {
          result.push_back(index[node]);
        }
        
        for(int i=0; i < trie[node].size(); ++i)
        {
          if(trie[node][i]!=0)
          {
            current.push_back('a'+i);
            dfs(trie[node][i], result, current);
            current.pop_back();
          }
        }
      }
      
      std::vector<int> find_all(const std::string& str)
      {
        std::vector<int> result;
        
        int node = 0;
        for(int i=0; i < str.size(); ++i)
        {
          // assume str is "ab" and we found "a" so far
          // then check if the rest of str("b") is palindrome
          if(index[node]!=-1 && is_palindrome(str, i, int(str.size())-1))
          {
            result.push_back(index[node]);
          }
          
          char c = str[i];
          if(trie[node][c-'a']==0)
            return result;
          
          node = trie[node][c-'a'];
        }
        
        // we fully matched str in trie. now search this node recursively to find palindromes underneath
        // ab ...... ba => any palindrome in between will result palindrome in overall
        std::string current;
        dfs(node, result, current);
        return result;
      }
    };
public:
    vector<vector<int>> palindromePairs(vector<string>& words) {

      Trie reverse;
      for(int i=0; i < words.size(); ++i)
      {
        auto word = words[i];
        std::reverse(word.begin(), word.end());
        reverse.insert(word, i);
      }
      
      std::vector<std::vector<int>> result;
      for(int i=0; i < words.size(); ++i)
      { 
        auto indexes = reverse.find_all(words[i]);
        for(int index: indexes)
        {
          if(index!=i)
          {
            result.push_back({i, index});
          }
        }
      }
      return result;
    }
};
419366