<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 Solution {
public:
    vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
        vector<string> ans;
        for(auto& word: words)
        {
            reverse(word.begin(), word.end());
        }
        sort(words.begin(), words.end(), [&](string& lhs, string& rhs) -> bool {
            return lhs.length() < rhs.length();
        });
        unordered_set<char> visChar;
        vector<unordered_set<string>> vis(10001);
        function<bool(string, bool)> isConcated = [&](string word, bool isFirst) -> bool {
            if (isFirst && word.length() <= 1)
            {
                return false;
            }
            if (vis[word.length()].find(word) != vis[word.length()].end())
            {
                return true;
            }
            for(int preLen = 1; preLen < word.length(); preLen++)
            {
                if (vis[preLen].find(word.substr(0, preLen)) != vis[preLen].end())
                {
                    if (isConcated(word.substr(preLen), false))
                    {
                        vis[word.substr(preLen).length()].insert(word.substr(preLen));
                        return true;
                    }
                }
            }
            return false;
        };
        for(string& word: words)
        {
            if (word.length() == 1)
            {
                vis[1].insert(word);
                visChar.insert(word[0]);
                continue;
            }
            {
                bool ctn = false;
                for(char& ch: word)
                {
                    if (visChar.find(ch) == visChar.end())
                    {
                        vis[word.length()].insert(word);
                        ctn = true;
                        break;
                    }
                }
                for(char& ch: word)
                {
                    visChar.insert(ch);
                }
                if (ctn)
                {
                    continue;
                }
            }
            {
                unordered_set<char> prev;
                for(char ch: word)
                {
                    prev.insert(ch);
                }
                if (prev.size() == 1 && vis[1].find(word.substr(0, 1)) != vis[1].end())
                {
                    ans.push_back(word);
                    for(int i = 2; i <= word.length(); i++)
                    {
                        vis[i].insert(word.substr(0, i));
                    }
                    continue;
                }
            }
            if (isConcated(word, true)) {
                ans.push_back(word);
            }
            vis[word.length()].insert(word);
        }
        for(auto& word: ans)
        {
            reverse(word.begin(), word.end());
        }
        return ans;
    }
};
125961