<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 SolutionUsingBinarySearch {
public:
    vector<vector<string>> suggestedProducts(
            vector<string>& products,
            string searchWord) {
        vector<vector<string>> ret;
        
        sort(products.begin(), products.end());
        
        string q = "";
        for (size_t i = 0; i < searchWord.size(); i++) {
            q += searchWord[i];
            
            auto it = lower_bound(products.begin(), products.end(), q);
            if (it == products.end()) {
                vector<string> results;
                ret.push_back(results);
                continue;
            }
            
            vector<string> results;
            size_t pos = it - products.begin();
            size_t endPos = min(pos + 3, products.size());
            for (size_t j = pos; j < endPos; j++) {
                string product = products[j];
                if (product.find(q) != 0) {
                    break;
                }
                
                results.push_back(product);
            }
            
            ret.push_back(results);
        }
        
        return ret;
    }
};

struct Node {
    map<char, Node> children;
    bool isWord;
};

class SolutionUsingTrie {
public:
    vector<vector<string>> suggestedProducts(
            vector<string>& products,
            string searchWord) {
        vector<vector<string>> ret;
        
        Node trie = buildTrie(products);
        
        string word = "";
        for (size_t i = 0; i < searchWord.size(); i++) {
            word += searchWord[i];
            
            vector<char> collectedChars;
            vector<string> collectedProducts;
            searchInTrie(trie, word, collectedChars, collectedProducts);
            
            ret.push_back(collectedProducts);
        }
        
        return ret;
    }
    
private:
    Node buildTrie(const vector<string>& products) {
        Node ret;
        ret.isWord = false;
                
        for (string product : products) {
            Node* currentNode = &ret;
            
            for (char c : product) {
                if (currentNode->children.find(c) == currentNode->children.end()) {
                    Node child;
                    child.isWord = false;
                    currentNode->children[c] = child;
                }
                
                currentNode = &(currentNode->children[c]);
            }
            
            currentNode->isWord = true;
        }
        
        return ret;
    }
    
    void searchInTrie(
            const Node& node,
            const string& word,
            const vector<char>& collectedChars,
            vector<string>& collectedProducts) {
        if (node.isWord && word.size() <= collectedChars.size()) {
            string product = "";
            for (char c : collectedChars) {
                product += c;
            }
            collectedProducts.push_back(product);
        }
        
        for (auto it = node.children.begin();
                it != node.children.end() && collectedProducts.size() < 3;
                it++) {
            const char cha = it->first;
            const Node& child = it->second;
            
            if (collectedChars.size() >= word.size() ||
                    word[collectedChars.size()] == cha) {
                vector<char> copy(collectedChars);
                copy.push_back(cha);
                searchInTrie(child, word, copy, collectedProducts);
            }
        }
    }
};

//class Solution : public SolutionUsingBinarySearch {};
class Solution : public SolutionUsingTrie {};

598630