<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{
    public:
    bool end=false;
    Trienode* *next =new Trienode* [26];
    Trienode(){
        for(int i=0 ; i<26 ;i++){
            this->next[i]=NULL;
        }
    }
};
class Trie{
    public:
    Trienode * root=new Trienode;
    void insert(Trienode * root,string word){
        if(!root->next[word[0]-'a']){
            root->next[word[0]-'a']=new Trienode;
        }
        if(word.size()==1){
            root->next[word[0]-'a']->end=true;
        }else{
            insert(root->next[word[0]-'a'],word.substr(1));
        }
    }
    bool search(Trienode * root,string word){
        if(!root->next[word[0]-'a']){
            return false;
        }else{
            if(word.size()==1){
                return root->next[word[0]-'a']->end;
            }else{
                return search(root->next[word[0]-'a'],word.substr(1));
            }
        }
        return false;
    }
    void add(string a){
        insert(root,a);
    }
    bool find(string a){
        return search(root,a);
    }
};


class Solution {
public:
    bool helper(string word,Trie * tri,Trie * combo,vector<int> & dp , int i){
        int n=word.size();
        if(i==n) return 1;
        if(dp[i]!=-1) return dp[i];
        if(i!=0 && combo->find(word.substr(i))) return dp[i]=1;
        for(int j=i;j<n;j++){
            if((tri->find(word.substr(i,j-i+1)) ) && ((i==0 && j==n-1) ? 0:1)){
                bool tmp=helper(word,tri,combo,dp,j+1);
                if(tmp){
                    combo->add(word);
                    return dp[i]=1;
                }
            }
        }
        return dp[i]=0;
    }

    vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
        vector<string> ans;
        Trie * tri=new Trie;
        for(auto it:words){
            tri->add(it);
        }
        Trie * combo= new Trie;// taki baad me same subword toh use without solving can say
        for(auto it:words){
            vector<int> dp(it.size(),-1);
            if(helper(it,tri,combo,dp,0)){
                ans.push_back(it);
            }
        }
        return ans;
    }
};
267373