-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSearchSuggestionSystem.java
156 lines (134 loc) · 4.19 KB
/
SearchSuggestionSystem.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*https://leetcode.com/problems/search-suggestions-system/*/
class TrieNode
{
TrieNode[] hash;
boolean isEndOfWord;
TrieNode()
{
hash = new TrieNode[26];
isEndOfWord = false;
}
}
class Trie
{
boolean stopDFS = false;
TrieNode root;
Trie()
{
root = new TrieNode();
}
public void add(String s)
{
TrieNode temp = root;
for (int i = 0; i < s.length(); ++i)
{
if (temp.hash[s.charAt(i)-'a'] == null)
temp.hash[s.charAt(i)-'a'] = new TrieNode();
temp = temp.hash[s.charAt(i)-'a'];
}
temp.isEndOfWord = true;
}
public List<List<String>> getWords(String key)
{
List<List<String>> result = new ArrayList<List<String>>();
//for each substring of the key
for (int i = 1; i <= key.length(); ++i)
{
//store the substring in curr
String curr = key.substring(0,i);
//mark flag true
boolean flag = true;
//declare two lists of strings
List<String> prevList = null, currList = new ArrayList<String>();
//if it is not the first substring
if (i > 1)
{
//get the previous list
prevList = result.get(result.size()-1);
//for each word in the previous list
for (String prevWord : prevList)
{
//if curr is not a substring of the word, mark flag as false
try{
if (prevWord.charAt(curr.length()-1) != curr.charAt(curr.length()-1))
flag = false;
}
catch(Exception e)
{
flag = false;
}
}
}
//if flag is still true and previous list is non-null
if (flag && prevList != null)
{
//copy all the elements of previous list to current list and add it to result
for (String word : prevList)
currList.add(word);
result.add(currList);
}
//otherwise
else
{
//move till the last letter of curr
TrieNode temp = root;
for (int j = 0; j < curr.length(); ++j)
temp = temp.hash[curr.charAt(j)-'a'];
//mark stopDFS as false
stopDFS = false;
//call DFS
runDFS(new StringBuffer(curr), temp, currList);
//add the current list to result
result.add(currList);
}
}
return result;
}
public void runDFS(StringBuffer curr, TrieNode temp, List<String> currList)
{
//if temp is not null and end of word is true
if (temp != null && temp.isEndOfWord)
{
//add curr to list, if the list size is 3, mark stopDFS as true and return
currList.add(curr.toString());
if (currList.size() == 3)
{
stopDFS = true;
return;
}
}
//if temp is null, return
else if (temp == null)
{
return;
}
//for each index
for (int i = 0; i < 26; ++i)
{
//if hash index is not null
if (temp.hash[i] != null)
{
//append the character to curr
curr.append((char)(i+'a'));
/*recursion call of DFS*/
runDFS(curr,temp.hash[i],currList);
//remove the last added character from curr
curr.delete(curr.length()-1, curr.length());
}
//if stopDFS is true, break
if (stopDFS) break;
}
}
}
class Solution {
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
List<List<String>> result;
//add all words to trie
Trie trie = new Trie();
for (String product : products)
trie.add(product);
//call get words function
result = trie.getWords(searchWord);
return result;
}
}