forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1268_Search_suggestion_system
139 lines (130 loc) · 4.57 KB
/
1268_Search_suggestion_system
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// https://leetcode.com/problems/search-suggestions-system/
// SOLUTION 2 - USING BINARY SEARCH
class Solution {
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
List<List<String>> ans = new ArrayList<>();
Arrays.sort(products);
for (int i = 1; i <= searchWord.length(); i ++) {
String cur = searchWord.substring(0, i);
int k = Arrays.binarySearch(products, cur);
while (k > 0 && cur.equals(products[k - 1]))
--k;
if (k < 0)
k = ~k;
List<String> suggestion = new ArrayList<>();
for (int j = k + 3; k < products.length && k < j && products[k].startsWith(cur); ++k )
suggestion.add(products[k]);
ans.add(suggestion);
}
return ans;
}
}
// SOLUTION 1 - USING TRIES
// class Solution {
// class Trie {
// Trie[] sub = new Trie[26];
// LinkedList<String> suggestion = new LinkedList<>();
// }
// public List<List<String>> suggestedProducts(String[] products, String searchWord) {
// Arrays.sort(products);
// Trie root= new Trie();
// for (String p : products) {
// insert(p, root);
// }
// return search(searchWord, root);
// }
// private void insert(String p, Trie root) {
// Trie t = root;
// for (char c : p.toCharArray()) {
// if (t.sub[c - 'a'] == null)
// t.sub[c - 'a'] = new Trie();
// t = t.sub[c - 'a'];
// if (t.suggestion.size() < 3)
// t.suggestion.offer(p);
// }
// }
// private List<List<String>> search(String searchWord, Trie root) {
// List<List<String>> ans = new ArrayList<>();
// for (char c : searchWord.toCharArray()) {
// if (root != null)
// root = root.sub[c - 'a'];
// ans.add(root == null ? Arrays.asList() : root.suggestion);
// }
// return ans;
// }
// }
// MY APPROACH
// class Trie {
// private class TrieNode {
// Map<Character,TrieNode> children;
// boolean endOfWord;
// TrieNode(){
// children = new HashMap<Character,TrieNode>();
// endOfWord = false;
// }
// }
// /** Initialize your data structure here. */
// public TrieNode root;
// public Trie() {
// root = new TrieNode();
// }
// /** Inserts a word into the trie. */
// public void insert(String word) {
// TrieNode current = root;
// for(int i=0;i<word.length();i++) {
// char ch = word.charAt(i);
// TrieNode node = current.children.get(ch);
// if(node == null) {
// node = new TrieNode();
// current.children.put(ch,node);
// }
// current = node;
// }
// current.endOfWord = true;
// }
// /** Returns if the word is in the trie. */
// public boolean search(String word) {
// TrieNode current = root;
// for(int i=0;i<word.length();i++) {
// char ch = word.charAt(i);
// TrieNode node = current.children.get(ch);
// if(node == null)
// return false;
// current = node;
// }
// return current.endOfWord;
// }
// /** Returns if there is any word in the trie that starts with the given prefix. */
// public boolean startsWith(String prefix) {
// TrieNode current = root;
// for(int i=0;i<prefix.length();i++) {
// char ch = prefix.charAt(i);
// TrieNode node = current.children.get(ch);
// if(node == null)
// return false;
// current = node;
// }
// return true;
// }
// }
// /**
// * Your Trie object will be instantiated and called as such:
// * Trie obj = new Trie();
// * obj.insert(word);
// * boolean param_2 = obj.search(word);
// * boolean param_3 = obj.startsWith(prefix);
// */
// class Solution {
// Trie trie = new Trie();
// public List<List<String>> suggestedProducts(String[] products, String searchWord) {
// for (String product : products)
// trie.insert(product);
// List<List<String>> result = new ArrayList<List<String>>();
// int len = searchWord.length();
// String prefix = "";
// for (int i = 0; i < len; i ++) {
// List<String> possible = new ArrayList<>(3);
// prefix += searchWord.charAt(i);
// }
// }
// }