<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 TrieNode {
public:
    char data;
    TrieNode *children[26];
    bool isTerminal;
    
    TrieNode(char ch) {
        data = ch;
        for(int i = 0; i < 26; i++) {
            children[i] = NULL;
        }
        isTerminal = false;
    }
};

class Trie {
    TrieNode *root;
    
public:
    Trie() {
        root = new TrieNode('\0');
    }

    void insertUtil(TrieNode* root, const string &word, int index) {
        if(index == word.length()) {
            root->isTerminal = true; 
            return;
        }

        int charIndex = word[index] - 'a';
        TrieNode* child;
        
        if(root->children[charIndex] != NULL) {
            child = root->children[charIndex];
        } else {
            child = new TrieNode(word[index]);
            root->children[charIndex] = child;
        }

        insertUtil(child, word, index + 1);
    }

    void insert(const string &word) {
        insertUtil(root, word, 0);
    }

    void printSuggestions(TrieNode* curr, vector<string> &temp, string &prefix) {
        if(curr->isTerminal) {
            temp.push_back(prefix);
        }
        for(char ch = 'a'; ch <= 'z'; ch++) {
            TrieNode* next = curr->children[ch - 'a'];
            if(next != NULL){
                prefix.push_back(ch);
                printSuggestions(next, temp, prefix);
                prefix.pop_back();
            }
        }
    }

    vector<vector<string>> getSuggestions(const string &str) {
        TrieNode* prev = root;
        vector<vector<string>> output;
        string prefix = "";

        for(int i = 0; i < str.length(); i++) {
            char lastch = str[i];
            prefix.push_back(lastch);
            // kya prev se attached lastch hain koi?
            TrieNode *curr = prev->children[lastch - 'a'];

            if(curr == NULL) {
                // If no more matches in the Trie, just push empty vectors for remaining characters
                while (i < str.length()) {
                    output.push_back({});
                    i++;
                }
                break;
            }

            vector<string> temp;
            printSuggestions(curr, temp, prefix);
            if(temp.size() > 3) {
                temp.resize(3); // Keep only top 3 suggestions
            }
            output.push_back(temp);
            prev = curr;
        }
        return output;
    }

    ~Trie() {
    }
};

class Solution {
public:
    vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
        Trie t; 
        // Insert all strings in trie
        for(string &str : products) {
            t.insert(str);
        }
        return t.getSuggestions(searchWord);
    }
};

100710