<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>

<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct)&nbsp;in the given array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;cat&quot;,&quot;cats&quot;,&quot;catsdogcats&quot;,&quot;dog&quot;,&quot;dogcatsdog&quot;,&quot;hippopotamuses&quot;,&quot;rat&quot;,&quot;ratcatdogcat&quot;]
<strong>Output:</strong> [&quot;catsdogcats&quot;,&quot;dogcatsdog&quot;,&quot;ratcatdogcat&quot;]
<strong>Explanation:</strong> &quot;catsdogcats&quot; can be concatenated by &quot;cats&quot;, &quot;dog&quot; and &quot;cats&quot;; 
&quot;dogcatsdog&quot; can be concatenated by &quot;dog&quot;, &quot;cats&quot; and &quot;dog&quot;; 
&quot;ratcatdogcat&quot; can be concatenated by &quot;rat&quot;, &quot;cat&quot;, &quot;dog&quot; and &quot;cat&quot;.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;cat&quot;,&quot;dog&quot;,&quot;catdog&quot;]
<strong>Output:</strong> [&quot;catdog&quot;]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= words[i].length &lt;= 30</code></li>
	<li><code>words[i]</code> consists of only lowercase English letters.</li>
	<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
	<li><code>1 &lt;= sum(words[i].length) &lt;= 10<sup>5</sup></code></li>
</ul>

class TrieNode:
    def __init__(self):
        self.child = [None]*26
        self.end = False

class Solution:
    def __init__(self):
        self.root = TrieNode()
        self.sent = []
        self.tab = {}
        
    def trieInsert(self, word):
        currNode = self.root
        for x in word:
            idx = ord(x) - ord('a')
            if not currNode.child[idx]:
                currNode.child[idx] = TrieNode()
            currNode = currNode.child[idx]
        currNode.end = True
        
    def search(self, word, s):
        if not s and word:
            self.sent.append((word, len(word)))
            self.tab[word] = True
        else:
            node = self.root
            for i in range(len(s)):
                if not self.sent:
                    idx = ord(s[i]) - ord('a')
                    if node.child[idx]:
                        word += chr(97 + idx)
                        if node.child[idx].end:
                            if s[i+1:] in self.tab.keys():
                                self.tab[word + s[i+1:]] = True
                                self.sent.append((word + s[i+1:], i))
                                break
    #                         print('w:', word, 's:', s[i+1:])
                            self.search(word, s[i+1:])
                        node = node.child[idx]
                    else:
                        break
    
    def findAllConcatenatedWordsInADict(self, words):
        for word in words:
            self.trieInsert(word)
            self.tab[word] = True
        ans = []
        for x in words:
            mk = False
            for i in range(len(x)-1, 0, -1):
                if x[i:] in self.tab:
                    mk = True
                    break
            if mk:
                self.sent.clear()
    #             print(x)
                self.search("", x)
#                 print(self.sent)
                if len(self.sent) > 1:
                    ans.append(x)
                elif len(self.sent) == 1:
                    if self.sent[0][1] < len(x):
                        ans.append(x)
    #             print('------------')
        return ans
        
32628