forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_1268.java
163 lines (149 loc) · 5.44 KB
/
_1268.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
157
158
159
160
161
162
163
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _1268 {
public static class Solution1 {
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
TrieNode root = buildTrie(products);
List<List<String>> result = new ArrayList<>();
for (int i = 1; i <= searchWord.length(); i++) {
result.add(findTopThreeMatches(root, searchWord.substring(0, i)));
}
return result;
}
private List<String> findTopThreeMatches(TrieNode root, String searchTerm) {
List<String> result = new ArrayList<>();
TrieNode node = root;
for (char c : searchTerm.toCharArray()) {
if (node.children[c - 'a'] == null) {
return result;
} else {
node = node.children[c - 'a'];
}
}
if (node.isWord) {
result.add(searchTerm);
}
for (TrieNode child : node.children) {
if (child != null) {
List<String> thisResult = dfs(child, searchTerm, new ArrayList<>());
result.addAll(thisResult);
if (result.size() >= 3) {
return result.subList(0, 3);
}
}
}
return result;
}
private List<String> dfs(TrieNode node, String substring, List<String> result) {
if (node.isWord) {
result.add(substring + node.c);
if (result.size() >= 3) {
return result;
}
}
for (TrieNode child : node.children) {
if (child != null) {
dfs(child, substring + node.c, result);
}
}
return result;
}
private TrieNode buildTrie(String[] products) {
TrieNode root = new TrieNode(' ');
for (String pro : products) {
insert(pro, root);
}
return root;
}
private void insert(String word, TrieNode root) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (node.children[c - 'a'] == null) {
node.children[c - 'a'] = new TrieNode(c);
}
node = node.children[c - 'a'];
}
node.isWord = true;
}
class TrieNode {
TrieNode[] children;
boolean isWord;
char c;
public TrieNode(char c) {
this.c = c;
this.children = new TrieNode[26];
}
}
}
public static class Solution2 {
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
TrieNode root = buildTrie(products);
List<List<String>> result = new ArrayList<>();
for (int i = 1; i <= searchWord.length(); i++) {
String searchTerm = searchWord.substring(0, i);
TrieNode tmp = root;
List<String> searchResult = new ArrayList<>();
for (int j = 0; j < searchTerm.length(); j++) {
char c = searchTerm.charAt(j);
if (tmp.children[c - 'a'] == null) {
break;
} else {
tmp = tmp.children[c - 'a'];
}
if (j == searchTerm.length() - 1) {
searchResult.addAll(findAllWords(tmp, searchTerm));
}
}
result.add(searchResult.size() > 3 ? searchResult.subList(0, 3) : searchResult);
}
return result;
}
private List<String> findAllWords(TrieNode trieNode, String prefix) {
List<String> result = new ArrayList<>();
if (trieNode.isWord) {
result.add(prefix);
if (result.size() > 3) {
return result;
}
}
for (TrieNode node : trieNode.children) {
if (node != null) {
result.addAll(findAllWords(node, prefix + node.val));
if (result.size() > 3) {
return result;
}
}
}
return result;
}
private TrieNode buildTrie(String[] words) {
TrieNode root = new TrieNode(' ');
for (String word : words) {
TrieNode tmp = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (tmp.children[c - 'a'] == null) {
tmp.children[c - 'a'] = new TrieNode(c);
}
tmp = tmp.children[c - 'a'];
if (i == word.length() - 1) {
tmp.isWord = true;
}
}
}
return root;
}
public class TrieNode {
TrieNode[] children;
char val;
boolean isWord;
public TrieNode(char val) {
this.children = new TrieNode[26];
this.val = val;
this.isWord = false;
}
}
}
}