<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p>

<p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p>

<p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p>

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

<pre>
<strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot;
<strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]]
<strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;].
After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;].
After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;].
</pre>

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

<pre>
<strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot;
<strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]]
<strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word.
</pre>

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

<ul>
	<li><code>1 &lt;= products.length &lt;= 1000</code></li>
	<li><code>1 &lt;= products[i].length &lt;= 3000</code></li>
	<li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li>
	<li>All the strings of <code>products</code> are <strong>unique</strong>.</li>
	<li><code>products[i]</code> consists of lowercase English letters.</li>
	<li><code>1 &lt;= searchWord.length &lt;= 1000</code></li>
	<li><code>searchWord</code> consists of lowercase English letters.</li>
</ul>

class Solution {

private:

struct LetterNode_st;

typedef struct ChildNode_st {
    struct ChildNode_st* next;
    struct LetterNode_st* child;
} ChildNode;

typedef struct LetterNode_st {
    char letter;
    bool is_word;
    ChildNode* children;
} LetterNode;

LetterNode* mRoot;

void createTree( vector<string>& products ){
    mRoot = nullptr;
    mRoot = (LetterNode*) malloc(sizeof(LetterNode));
    mRoot->letter = '\n';
    mRoot->children = nullptr;

    LetterNode* tmpNL = nullptr;
    LetterNode* tmpL = nullptr;
    ChildNode* tmpNC = nullptr;
    ChildNode* tmpC = nullptr;
    for( auto itw = products.begin(); itw != products.end(); ++itw ){
        string word = *itw;
        tmpL = mRoot;
        //cout << "word = " << word << endl;
        for( char chara : word ) {
            tmpC = tmpL->children;
            if(tmpC == nullptr) {
                //cout << "Letter " << tmpL->letter << " has no children, adding " << chara << " as child" << endl;
                tmpNL = (LetterNode*) malloc(sizeof(LetterNode));
                tmpNL->letter = chara;
                tmpNL->is_word = false;
                tmpNL->children = nullptr;
                
                tmpC = (ChildNode*) malloc(sizeof(ChildNode));
                tmpC->child = tmpNL;
                tmpC->next = nullptr;
                tmpL->children = tmpC;

                tmpL = tmpNL;
                continue;
            }

            while( tmpC->next != nullptr && tmpC->child->letter < chara ) {
                //cout << "Moving to letter " << tmpC->child->letter << endl;
                tmpC = tmpC->next;
            }

            if(tmpC->child->letter == chara ){
                //cout << "Letter " << chara << " exists, following the letter" << endl;
                tmpL = tmpC->child;
                continue;
            }
            if(tmpC->child->letter > chara){
                //cout << "Letter " << chara << " does not exists, adding it in the right spot" << endl;

                tmpNC = (ChildNode*) malloc(sizeof(ChildNode));
                tmpNC->child = tmpC->child;
                tmpNC->next = tmpC->next;
                tmpC->next = tmpNC;

                tmpNL = (LetterNode*) malloc(sizeof(LetterNode));
                tmpNL->letter = chara;
                tmpNL->is_word = false;
                tmpNL->children = nullptr;
                tmpC->child = tmpNL;

                tmpL = tmpC->child;
                continue;
            }
            
            if(tmpC->next == nullptr){
                //cout << "Letter " << chara << " does not exists, adding it at the end" << endl;
                tmpNL = (LetterNode*) malloc(sizeof(LetterNode));
                tmpNL->letter = chara;
                tmpNL->is_word = false;
                tmpNL->children = nullptr;

                tmpNC = (ChildNode*) malloc(sizeof(ChildNode));
                tmpNC->child = tmpNL;
                tmpNC->next = nullptr;
                tmpC->next = tmpNC;

                tmpL = tmpNC->child;
            }
        }
        tmpL->is_word = true;
    }
}

void takeThreeWords(LetterNode* startL, string baseWord, vector<string> &words){
    if( words.size() == 3 ){
        return;
    }
    if( startL == nullptr ){
        return;
    }
    if( startL->is_word ){
        words.push_back(baseWord);
    }
    ChildNode* tmpC = startL->children;
    while( tmpC!=nullptr && words.size() < 3 ){
        //cout << "baseWord = " << baseWord << endl;
        //cout << "tmpC->child->letter = " << tmpC->child->letter << endl;
        takeThreeWords(
            tmpC->child,
            baseWord+tmpC->child->letter,
            words);
        tmpC = tmpC->next;
    }

}

vector<string> searchFor(string searchWord){
    LetterNode* tmpL = mRoot;
    ChildNode* tmpC = nullptr;
    for( char chara : searchWord ){

        tmpC = tmpL->children;
        if(tmpC == nullptr) {
            tmpL = nullptr;
            break;
        }

        while( tmpC->next != nullptr && tmpC->child->letter < chara ) {
            tmpC = tmpC->next;
        }

        if(tmpC->child->letter == chara ){
            tmpL = tmpC->child;
            continue;
        }
        tmpL = nullptr;
        break;
    }

    /*
    cout << "searchWord = " << searchWord << endl;
    if(tmpL != nullptr){
        cout << "tmpL->letter = " << tmpL->letter << endl;
    } else {
        cout << "tmpL->letter is empty" << endl;
    }
    */

    // tmpL contains the last matched letter
    vector<string> words;
    takeThreeWords(tmpL, searchWord, words);

    return words;

}

public:
    vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
        
        createTree( products );

        string partialWord = "";
        vector<vector<string>> sol;
        for( size_t idx = 0; idx < searchWord.length(); ++idx ) {
            partialWord += searchWord[idx];
            sol.push_back(searchFor(partialWord));
        }
        return sol;
    }
};
497490