<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 Solution {
public:
    string longestWord(vector<string>& words) {
        Trie* trie = new Trie();

        for(auto word: words) {
            trie->insert(word);
        }
        string longestWord = "";

        for(auto word: words) {
            if(trie->canBeBuilt(word)) {
                if(word.size() > longestWord.size() || (word.size() == longestWord.size() && word < longestWord)) {
                    longestWord = word;
                }
            }
        }
        return longestWord;
    }

private:
class Trie {
public:
    Trie() {
        root.resize(26, nullptr);
        setEnd(false);
    }
    
    void insert(string word) {
        Trie* node = this;
        for(int index=0; index < word.size(); ++index) {
            if(!node->containsChar(word[index])) {
                node->putChar(word[index], new Trie());
            }
            node = node->getNode(word[index]);
        }
        node->setEnd(true);
    }
    
    bool search(string word) {
        Trie* node = this;
        for(int index=0; index < word.size(); ++index) {
            if(!node->containsChar(word[index])) {
                return false;
            }
            node = node->getNode(word[index]);
        }

        return node->isEnd();
    }
    
    bool startsWith(string prefix) {
        Trie* node = this;
        for(int index=0; index < prefix.size(); ++index) {
            if(!node->containsChar(prefix[index])) {
                return false;
            }
            node = node->getNode(prefix[index]);
        }

        return true;        
    }

    bool canBeBuilt(string word) {
        Trie* node = this;

        if(node->getNode(word[0]) == nullptr)
            return false;

        for(int index=0; index < word.size()-1; ++index) {
            if(node->getNode(word[index]) == nullptr || !node->getNode(word[index])->isEnd())
                return false;
            node = node->getNode(word[index]);
        }

        return true;
    }

    bool isEnd() {
        return end;
    }

    void setEnd(bool end) {
        this->end = end;
    }

    bool containsChar(char c) {
        return getNode(c) != nullptr;
    }

    void putChar(char c, Trie* node) {
        root[c-'a'] = node;
    }

    Trie* getNode(char c) {
        return root[c-'a'];
    }

private: 
    vector<Trie*> root;
    bool end;
};
};
38815