<p>Given an array of strings <code>words</code> representing an English Dictionary, return <em>the longest word in</em> <code>words</code> <em>that can be built one character at a time by other words in</em> <code>words</code>.</p>

<p>If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.</p>

<p>Note that the word should be built from left to right with each additional character being added to the end of a previous word.&nbsp;</p>

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

<pre>
<strong>Input:</strong> words = [&quot;w&quot;,&quot;wo&quot;,&quot;wor&quot;,&quot;worl&quot;,&quot;world&quot;]
<strong>Output:</strong> &quot;world&quot;
<strong>Explanation:</strong> The word &quot;world&quot; can be built one character at a time by &quot;w&quot;, &quot;wo&quot;, &quot;wor&quot;, and &quot;worl&quot;.
</pre>

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

<pre>
<strong>Input:</strong> words = [&quot;a&quot;,&quot;banana&quot;,&quot;app&quot;,&quot;appl&quot;,&quot;ap&quot;,&quot;apply&quot;,&quot;apple&quot;]
<strong>Output:</strong> &quot;apple&quot;
<strong>Explanation:</strong> Both &quot;apply&quot; and &quot;apple&quot; can be built from other words in the dictionary. However, &quot;apple&quot; is lexicographically smaller than &quot;apply&quot;.
</pre>

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

<ul>
	<li><code>1 &lt;= words.length &lt;= 1000</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 30</code></li>
	<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>

class TrieNode {
public:
    unordered_map<char, TrieNode*> mp;
    bool isEnd;
    TrieNode() {
        isEnd = false;
    }
};
class Trie {
public:
    TrieNode* root;
    Trie() {
        this->root = new TrieNode();
    }
    
    void insert(string word) {
        TrieNode* curr = root;
        for(char ch: word){
            if(curr->mp.count(ch) == 0){
                curr->mp[ch] = new TrieNode();
            }
            curr = curr->mp[ch];
        }
        curr->isEnd = true;
    }
    
    bool search(string word) {
        TrieNode* curr = root;
        for(char ch: word){
            if(curr->mp.find(ch) != curr->mp.end()){
                curr = curr->mp[ch];
            }
            else{
                return false;
            }
        }
        return curr->isEnd;
    }
    
    bool startsWith(string prefix) {
        TrieNode* curr = root;
        for(char ch: prefix){
            if(curr->mp.find(ch) != curr->mp.end()){
                curr = curr->mp[ch];
            }
            else{
                return false;
            }
        }
        return true;
    }
};

class Solution {
public:
    string longestWord(vector<string>& words) {
        //using quewe to amke sure all the strings which may be a posssibble answer is maintained and 
        //iterated again and again. and if it f=stops at some point throw it out 
        Trie* trie = new Trie();

        for(auto word: words){
            trie->insert(word);
        }
        queue<pair<TrieNode*,string>> q;
        TrieNode* root = trie->root;
        string temp = "";
        for(auto ch = 'a'; ch<='z'; ch++){
            if(root->mp.count(ch) == 1 and root->mp[ch]->isEnd){
                temp.push_back(ch);
                q.push({root->mp[ch], temp});
                temp.pop_back();
            }
        }
        vector<string> ans;
        while(!q.empty()){
            int n = q.size();
            vector<string> temp;
            while(n--){
                TrieNode* curr = q.front().first;
                string str = q.front().second; q.pop();
                temp.push_back(str);
                cout<<str<<" ";
                for(auto ch = 'a'; ch<='z'; ch++){
                    if(curr->mp.count(ch) == 1 and curr->mp[ch]->isEnd){
                        q.push({curr->mp[ch], str + ch});
                    }
                }
            }
            cout<<endl;
            if(temp.size() == 0){
                break;
            }
            else{
                ans = temp;
            }
        }
        sort(ans.begin(), ans.end());
        return ans.size() == 0 ? "" : ans[0] ;
    }
};
40409