Skip to content

Commit 29089bb

Browse files
add 1268
1 parent c0356e3 commit 29089bb

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ _If you like this project, please leave me a star._ ★
187187
|1275|[Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1275.java) | |Easy|Array|
188188
|1273|[Delete Tree Nodes](https://leetcode.com/problems/delete-tree-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1273.java) | |Medium|Dynamic Programming, DFS |
189189
|1271|[Hexspeak](https://leetcode.com/problems/hexspeak/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1271.java) | |Easy||
190+
|1268|[Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1268.java) | |Medium|String|
190191
|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1267.java) | |Medium||
191192
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | |Easy||
192193
|1265|[Print Immutable Linked List in Reverse](https://leetcode.com/problems/print-immutable-linked-list-in-reverse//)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1265.java) | |Medium||
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1268 {
7+
public static class Solution1 {
8+
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
9+
TrieNode root = buildTrie(products);
10+
List<List<String>> result = new ArrayList<>();
11+
for (int i = 1; i <= searchWord.length(); i++) {
12+
String searchTerm = searchWord.substring(0, i);
13+
result.add(findTopThreeMatches(root, searchTerm));
14+
}
15+
return result;
16+
}
17+
18+
private List<String> findTopThreeMatches(TrieNode root, String searchTerm) {
19+
List<String> result = new ArrayList<>();
20+
TrieNode node = root;
21+
for (char c : searchTerm.toCharArray()) {
22+
if (node.children[c - 'a'] == null) {
23+
return result;
24+
} else {
25+
node = node.children[c - 'a'];
26+
}
27+
}
28+
if (node.isWord) {
29+
result.add(searchTerm);
30+
}
31+
for (TrieNode child : node.children) {
32+
if (child != null) {
33+
List<String> thisResult = dfs(child, searchTerm, new ArrayList<>());
34+
result.addAll(thisResult);
35+
if (result.size() >= 3) {
36+
return result.subList(0, 3);
37+
}
38+
}
39+
}
40+
return result;
41+
}
42+
43+
private List<String> dfs(TrieNode node, String substring, List<String> result) {
44+
if (node.isWord) {
45+
result.add(substring + node.c);
46+
if (result.size() >= 3) {
47+
return result;
48+
}
49+
}
50+
for (TrieNode child : node.children) {
51+
if (child != null) {
52+
dfs(child, substring + node.c, result);
53+
}
54+
}
55+
return result;
56+
}
57+
58+
private TrieNode buildTrie(String[] products) {
59+
TrieNode root = new TrieNode(' ');
60+
for (String pro : products) {
61+
insert(pro, root);
62+
}
63+
return root;
64+
}
65+
66+
private void insert(String word, TrieNode root) {
67+
TrieNode node = root;
68+
for (int i = 0; i < word.length(); i++) {
69+
char c = word.charAt(i);
70+
if (node.children[c - 'a'] == null) {
71+
node.children[c - 'a'] = new TrieNode(c);
72+
}
73+
node = node.children[c - 'a'];
74+
}
75+
node.isWord = true;
76+
}
77+
78+
class TrieNode {
79+
TrieNode[] children = new TrieNode[26];
80+
boolean isWord;
81+
char c;
82+
83+
public TrieNode(char c) {
84+
this.c = c;
85+
}
86+
}
87+
}
88+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1268;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1268Test {
13+
private static _1268.Solution1 solution1;
14+
private static List<List<String>> expected;
15+
private static String[] products;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _1268.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
products = new String[]{"mobile", "mouse", "moneypot", "monitor", "mousepad"};
25+
expected = Arrays.asList(Arrays.asList("mobile", "moneypot", "monitor"), Arrays.asList("mobile", "moneypot", "monitor"), Arrays.asList("mouse", "mousepad"), Arrays.asList("mouse", "mousepad"), Arrays.asList("mouse", "mousepad"));
26+
assertEquals(expected, solution1.suggestedProducts(products, "mouse"));
27+
}
28+
}

0 commit comments

Comments
 (0)