-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchSuggestionSystem.java
More file actions
49 lines (44 loc) · 2.23 KB
/
Copy pathSearchSuggestionSystem.java
File metadata and controls
49 lines (44 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
/* 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"]
*/
public class SearchSuggestionSystem {
class Trie {
Trie[] links = new Trie[26];
LinkedList<String> suggestion = new LinkedList<>();
}
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
Arrays.sort(products); // sort products.
Trie root = new Trie();
for (String p : products) { // build Trie.
Trie t = root;
for (char c : p.toCharArray()) { // insert current product into Trie.
if (t.links[c - 'a'] == null) {
t.links[c - 'a'] = new Trie();
}
t = t.links[c - 'a'];
if (t.suggestion.size() < 3) // maintain 3 lexicographically minimum strings.
t.suggestion.offer(p); // put products with same prefix into suggestion list.
}
}
List<List<String>> solution = new ArrayList<>();
for (char c : searchWord.toCharArray()) { // search product.
if (root != null) // if there exist products with current prefix.
root = root.links[c - 'a'];
solution.add(root == null ? Arrays.asList() : root.suggestion); // add it if there exist products with current prefix.
}
return solution;
}
}