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.
1import java.util.*;
2
3public class SearchSuggestions {
4 public List<List<String>> suggestedProducts(String[] products, String searchWord) {
5 Arrays.sort(products);
6 List<List<String>> result = new ArrayList<>();
7 String prefix = "";
8 for (char c : searchWord.toCharArray()) {
9 prefix += c;
10 List<String> matches = new ArrayList<>();
11 for (String product : products) {
12 if (product.startsWith(prefix)) {
13 matches.add(product);
14 if (matches.size() == 3) break;
15 }
16 }
17 result.add(matches);
18 }
19 return result;
20 }
21
22 public static void main(String[] args) {
23 String[] products = {"mobile","mouse","moneypot","monitor","mousepad"};
24 String searchWord = "mouse";
25 SearchSuggestions ss = new SearchSuggestions();
26 System.out.println(ss.suggestedProducts(products, searchWord));
27 }
28}
29
This Java solution also starts by sorting the products array. It constructs the prefix for each character of the searchWord and collects up to three products beginning with that prefix. The sorting ensures lexicographical order.
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.
1using System.Collections.Generic;
class TrieNode {
public Dictionary<char, TrieNode> Children = new Dictionary<char, TrieNode>();
public List<string> Suggestions = new List<string>();
}
class Solution {
public void Insert(string product, TrieNode node) {
foreach (char c in product) {
if (!node.Children.ContainsKey(c)) {
node.Children[c] = new TrieNode();
}
node = node.Children[c];
if (node.Suggestions.Count < 3) {
node.Suggestions.Add(product);
}
}
}
public IList<IList<string>> SuggestedProducts(string[] products, string searchWord) {
Array.Sort(products);
TrieNode root = new TrieNode();
foreach (string product in products) {
Insert(product, root);
}
List<IList<string>> result = new List<IList<string>>();
TrieNode node = root;
foreach (char c in searchWord) {
if (node != null) {
node = node.Children.ContainsKey(c) ? node.Children[c] : null;
}
result.Add(node != null ? new List<string>(node.Suggestions) : new List<string>());
}
return result;
}
static void Main() {
string[] products = {"mobile","mouse","moneypot","monitor","mousepad"};
string searchWord = "mouse";
Solution solution = new Solution();
var result = solution.SuggestedProducts(products, searchWord);
foreach (var list in result) {
Console.WriteLine(string.Join(" ", list));
}
}
}
Using a Trie, this C# implementation tracks each node with appropriate top three suggestions. Nodes correspond to characters in product names. We then fetch results by prefix, using the Trie to index.