Sponsored
Sponsored
This approach involves sorting the products array first and then iteratively constructing prefixes from the searchWord. For each prefix, use a simple linear search over the sorted products array to find matches. By keeping the products sorted, it ensures that we can easily pick the smallest lexicographical options.
After sorting, for every prefix of the searchWord, we traverse the sorted products list, check if the product starts with the current prefix, and collect up to three matches.
Time Complexity: O(n log n) due to sorting, where n is the length of products. Each prefix search is O(n), leading to an overall complexity of O(n log n + m * n), where m is the length of searchWord.
Space Complexity: O(1) additional space is needed outside the result storage, although storing results may take O(m * 3) space.
1def suggestedProducts(products, searchWord):
2 products.sort()
3 result = []
4 prefix = ''
5 for char in searchWord:
6 prefix += char
7 matches = [product for product in products if product.startswith(prefix)]
8 result.append(matches[:3])
9 return result
10
11# Example usage:
12products = ["mobile","mouse","moneypot","monitor","mousepad"]
13searchWord = "mouse"
14print(suggestedProducts(products, searchWord))
This Python code starts by sorting the list of products. As we iterate through each character of the searchWord, it builds the prefix and filters products that start with the current prefix. We then select the first three such products.
This approach aims at using a Trie to efficiently handle prefix matching. With a Trie, we insert all product names into the Trie. As each character is typed in the searchWord, we traverse the Trie to check for the top three lexicographical matches.
Using a Trie allows us to handle the prefix matching efficiently by navigating through the structure step by step according to the current prefix.
Time Complexity: O(n m) to insert all products (where n is number of products and m is max product length), and O(k) to search for each prefix (where k is the length of searchWord).
Space Complexity: O(n m) for the Trie storage, since we store each character of every word.
1#include <vector>
#include <map>
#include <algorithm>
using namespace std;
struct TrieNode {
map<char, TrieNode*> children;
vector<string> suggestions;
};
class Solution {
public:
void insert(string product, TrieNode* node) {
for (char c : product) {
if (!node->children.count(c)) {
node->children[c] = new TrieNode();
}
node = node->children[c];
if (node->suggestions.size() < 3) {
node->suggestions.push_back(product);
}
}
}
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
sort(products.begin(), products.end());
TrieNode* root = new TrieNode();
for (string& product : products) {
insert(product, root);
}
vector<vector<string>> result;
TrieNode* node = root;
for (char c : searchWord) {
if (node) {
node = node->children.count(c) ? node->children[c] : nullptr;
}
result.push_back(node ? node->suggestions : vector<string>());
}
return result;
}
};
int main() {
vector<string> products = {"mobile","mouse","moneypot","monitor","mousepad"};
string searchWord = "mouse";
Solution solution;
vector<vector<string>> result = solution.suggestedProducts(products, searchWord);
for (auto &list : result) {
for (auto &prod : list) {
cout << prod << " ";
}
cout << endl;
}
return 0;
}
In C++, this solution uses a Trie for prefix matching. Each node in the Trie stores, at most, three suggested products. The suggestions are gathered based on prefix matches by traversing the Trie.