4. Search Suggestions System

Given an array of strings products and a string searchWord. We want to design a system that suggests at most three product names from products after each character of searchWord is typed. Suggested products should have common prefix with the searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.

Return list of lists of the suggested products after each character of searchWord is typed.

Example 1:

Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
Output: [
["mobile","moneypot","monitor"],
["mobile","moneypot","monitor"],
["mouse","mousepad"],
["mouse","mousepad"],
["mouse","mousepad"]
]
Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"]
After typing m and mo all products match and we show user ["mobile","moneypot","monitor"]
After typing mou, mous and mouse the system suggests ["mouse","mousepad"]

Example 2:

Input: products = ["havana"], searchWord = "havana"
Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]

Example 3:

Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]

Example 4:

Input: products = ["havana"], searchWord = "tatiana"
Output: [[],[],[],[],[],[],[]]

Solution: (Trie + DFS)

class Solution
{
public:
    struct TrieNode
    {
        char val;
        int endHere;
        int count;
        string k;
        TrieNode *child[26];
    };

    TrieNode *createNode(char val)
    {

        TrieNode *temp = new TrieNode;
        temp->val = val;
        temp->endHere = 0;
        temp->count = 0;
        temp->k = "";

        for (int i = 0; i < 26; i++)
        {
            temp->child[i] = NULL;
        }

        return temp;
    }

    void addNode(string s, TrieNode *root)
    {
        TrieNode *p = root;

        for (int i = 0; i < s.length(); i++)
        {
            int idx = s[i] - 97;
            if (p->child[idx])
            {
                p->child[idx]->count++;
            }
            else
            {
                p->child[idx] = createNode(s[i]);
            }

            p = p->child[idx];
        }

        p->endHere++;
        p->k = s;

        return;
    }

    void find(TrieNode *root, vector<string> &v, string res)
    {

        if (v.size() == 3)
        {
            return;
        }

        if (root->endHere > 0)
        {
            v.push_back(res);
        }

        for (int i = 0; i < 26; i++)
        {
            if (root->child[i])
            {
                char ch = i + 97;
                find(root->child[i], v, res + ch);
            }
        }

        return;
    }

    vector<vector<string>> suggestedProducts(vector<string> &products, string searchWord)
    {
        vector<vector<string>> res;

        TrieNode *root = createNode('/');

        for (int i = 0; i < products.size(); i++)
        {
            addNode(products[i], root);
        }

        string prefix = "";
        int i = 0;
        TrieNode *p = root;
        for (i = 0; i < searchWord.length(); i++)
        {
            vector<string> v;
            prefix += searchWord[i];

            int id = prefix[prefix.length() - 1] - 97;
            if (p->child[id])
            {
                p = p->child[id];
                find(p, v, prefix);
            }
            else
            {
                break;
            }

            res.push_back(v);
        }

        if (i != searchWord.length())
        {
            for (int k = i; k < searchWord.length(); k++)
            {
                res.push_back({});
            }
        }

        return res;
    }
};

Last updated