<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 LCRSTrie {
public:
    struct LCRSTrieNode {
        char ch;
        bool marked;
        int16_t wordIndex;
        int32_t child;
        int32_t next;
    };

    vector<LCRSTrieNode> nodePool;
    int32_t headIndex;

    int32_t createNode(char ch, bool marked, int16_t wordIndex) {
        this->nodePool.push_back({ ch, marked, wordIndex, -1, -1 });
        return static_cast<int>(this->nodePool.size() - 1);
    }

    void insert(std::string_view str, int16_t wordIndex) {
        assert(wordIndex >= 0);
        if (str.length() == 0) return;
        if (nodePool.size() == 0) {
            (void)this->createNode(str[0], false, wordIndex);
            this->headIndex = 0;
        }

        assert(this->headIndex >= 0 && this->headIndex < this->nodePool.size());
        if (str[0] < this->nodePool[this->headIndex].ch) {
            const int32_t oldHeadIndex = this->headIndex;
            this->headIndex = this->createNode(str[0], false, wordIndex);
            this->nodePool[this->headIndex].next = oldHeadIndex;
        }

        uint32_t strIndex = 0;
        int32_t nodeIndex = this->headIndex;

        while (strIndex < str.length() && nodeIndex >= 0 && nodeIndex < nodePool.size()) {
            if (str[strIndex] == this->nodePool[nodeIndex].ch) {
                if (strIndex == str.length() - 1) {
                    this->nodePool[nodeIndex].marked = true;
                    this->nodePool[nodeIndex].wordIndex = wordIndex;
                    return;
                }
                // move on to next level

                if (this->nodePool[nodeIndex].child < 0 || str[strIndex + 1] < this->nodePool[this->nodePool[nodeIndex].child].ch) {
                    const int32_t oldLLHead = this->nodePool[nodeIndex].child;
                    this->nodePool[nodeIndex].child = this->createNode(str[strIndex + 1], false, wordIndex);
                    this->nodePool[this->nodePool[nodeIndex].child].next = oldLLHead;
                }

                nodeIndex = this->nodePool[nodeIndex].child;
                strIndex++;
            } else if (str[strIndex] > this->nodePool[nodeIndex].ch) {
                // nodeIndex should always be at the beginning node of an LCRS Linked List sibling path
                int32_t prevIndex = nodeIndex;
                int32_t currIndex = this->nodePool[nodeIndex].next;

                while (currIndex >= 0 && currIndex < this->nodePool.size() && str[strIndex] > this->nodePool[currIndex].ch) {
                    prevIndex = currIndex;
                    currIndex = this->nodePool[currIndex].next;
                }

                if (currIndex < 0 || this->nodePool[currIndex].ch != str[strIndex]) {
                    assert(this->nodePool[prevIndex].ch < str[strIndex]);
                    assert(currIndex == -1 || this->nodePool[currIndex].ch > str[strIndex]);
                    
                    const int32_t newNodeIndex = this->createNode(str[strIndex], false, wordIndex);
                    if (prevIndex >= 0) {
                        this->nodePool[prevIndex].next = newNodeIndex;
                    }

                    this->nodePool[newNodeIndex].next = currIndex; 
                    nodeIndex = newNodeIndex;
                } else {
                    assert(this->nodePool[currIndex].ch == str[strIndex]);
                    nodeIndex = currIndex;
                }
            } else if (str[strIndex] < this->nodePool[nodeIndex].ch) {
                assert(0 && "Can only traverse forward or down through Left-Child-Right-Sibling Trie... Some error has occured...");
            }
        }
    }

    void findLongestBuiltCharByCharDFS(int32_t nodeIndex, int32_t depth, int16_t& bestWordIndex, int32_t& bestDepth) {
        while (nodeIndex >= 0 && nodeIndex < this->nodePool.size()) {
            if (this->nodePool[nodeIndex].marked) {
                findLongestBuiltCharByCharDFS(this->nodePool[nodeIndex].child, depth + 1, bestWordIndex, bestDepth);
                if (depth > bestDepth) {
                    bestWordIndex =  this->nodePool[nodeIndex].wordIndex;
                    bestDepth = depth;
                }
            }

            nodeIndex = this->nodePool[nodeIndex].next;
        }
    }

    int16_t findLongestBuiltCharByChar() {
        int32_t bestDepth = -1;
        int16_t bestWordIndex = -1;
        findLongestBuiltCharByCharDFS(this->headIndex, 0, bestWordIndex, bestDepth);
        return bestWordIndex;
    }
};

class Solution {
public:
    string longestWord(vector<string>& words) {
        LCRSTrie trie;
        assert(words.size() <= std::numeric_limits<int16_t>::max());
        for (size_t i = 0; i < words.size(); i++) {
            trie.insert(words[i], i);
        }
        const int16_t wordIndex = trie.findLongestBuiltCharByChar();
        return wordIndex >= 0 && wordIndex < words.size() ? words[wordIndex] : "";
    }
};
19690