1268. Search Suggestions System

1. Description

You are given an array of strings products and a string searchWord.
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 searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.
Return a list of lists of the suggested products after each character of searchWord is typed.

2. Example

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”]]
Explanation: The only word “havana” will be always suggested while typing the search word.

3. Constraints

  • 1 <= products.length <= 1000
  • 1 <= products[i].length <= 3000
  • 1 <= sum(products[i].length) <= 2 * $10^4$
  • All the strings of products are unique.
  • products[i] consists of lowercase English letters.
  • 1 <= searchWord.length <= 1000
  • searchWord consists of lowercase English letters.

4. Solutions

m = sum(products[i].size()), n = searchWord.size()
Time complexity: O(m + n)
Space complexity: O(m)

class Solution {
public:
    vector<vector<string>> suggestedProducts(
        const vector<string> &products,
        const string &searchWord) {
        for (auto product : products) {
            Node *iter = root.get();
            for (auto letter : product) {
                int idx = index(letter);
                if (iter->children[idx] == nullptr) {
                    iter->children[idx] = make_unique<Node>();
                }

                iter = iter->children[idx].get();
            }
            iter->is_end = true;
        }

        string current_result;
        vector<vector<string>> search_results(searchWord.size(), vector<string>{});
        auto iter = root.get();
        for (int i = 0; i < searchWord.size(); ++i) {
            auto &bucket = search_results[i];
            if (iter->children[index(searchWord[i])] != nullptr) {
                iter = iter->children[index(searchWord[i])].get();
                current_result.push_back(searchWord[i]);

                search_products(iter, current_result, bucket);
            } else {
                break;
            }
        }

        return search_results;
    }

private:
    struct Node {
        array<unique_ptr<Node>, 26> children{};
        bool is_end = false;
    };

    unique_ptr<Node> root = make_unique<Node>();

    static int index(char letter) {
        return letter - 'a';
    }

    void search_products(Node *iter, string &current_result, vector<string> &current_results)
        const {
        if (iter->is_end) {
            current_results.push_back(current_result);
        }

        if (current_results.size() < 3) {
            for (char letter = 'a'; letter <= 'z'; ++letter) {
                if (iter->children[index(letter)] != nullptr) {
                    current_result.push_back(letter);
                    search_products(
                        iter->children[index(letter)].get(), current_result, current_results);
                    current_result.pop_back();

                    if (current_results.size() == 3) {
                        break;
                    }
                }
            }
        }
    }
};
comments powered by Disqus