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

"""
08:23
naive: combination and checking in hash if string exists. 2^n
alternatively: build trie and dfs on word such that any time we reached prefix that is a word
    start from the top. avoid returning the same word exactly.

phase 1: build trie
phase 2: search for each word
    look for each word for concatenatd words.
    num_of_words = 0. each time reached to is word -> num of words += 1
    if num_of_words > 1 can use hash for last word. 

    search: 
        when reach isWord:
            - continue ("catsdog" -> "cats" , not "cat" prefix)
            - start new search with i+1
        complexity: in each stage can start new search with i+1 steps. -> l^2 operation for n words.
    def is_concatenated_word(word, i)
    def is_word
"""
class TrieNode:
    def __init__(self, char: str, isWord: bool = False):
        self.char = char
        self.isWord = isWord
        self.children = {} 

class Solution:
    def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
        words_set = set(words)
        @cache
        def is_concatenated_word(word: str, trie_head: TrieNode, char_index: int = 0) -> bool:
            node = trie_head
            while node and char_index < len(word):
                if node.isWord:
                    if word[char_index:] in words_set: # char index won't be 0 becuase head is not a word.
                        return True
                    if is_concatenated_word(word, trie_head, char_index):
                        return True
                
                if word[char_index] not in node.children:
                    return False

                node = node.children[word[char_index]]
                char_index += 1
            
            return False


        def build_word_trie(words) -> TrieNode:
            trie_head = node = TrieNode("")
            for word in words:
                node = trie_head
                for char in word:
                    if char not in node.children:
                        node.children[char] = TrieNode(char)
                    node = node.children[char]
                node.isWord = True
            return trie_head


        trie_head = build_word_trie(words)
        return [word for word in words if is_concatenated_word(word, trie_head)]
        

"""
            is_concatenated_word
            catsdogcats, 0 -> 1    2  3 4
            node = head ->  (c) ->(a) t s
            is_concatenated_word("sdogcats")-> false
                is_concatenated_word("dogcats") -> True

build_word_trie
    ["cat","dog","catdog"]
    h -> (c: (a: (t: (true,)))), (d:...)
"""
39634