Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit29089bb

Browse files
add 1268
1 parentc0356e3 commit29089bb

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.List;
5+
6+
publicclass_1268 {
7+
publicstaticclassSolution1 {
8+
publicList<List<String>>suggestedProducts(String[]products,StringsearchWord) {
9+
TrieNoderoot =buildTrie(products);
10+
List<List<String>>result =newArrayList<>();
11+
for (inti =1;i <=searchWord.length();i++) {
12+
StringsearchTerm =searchWord.substring(0,i);
13+
result.add(findTopThreeMatches(root,searchTerm));
14+
}
15+
returnresult;
16+
}
17+
18+
privateList<String>findTopThreeMatches(TrieNoderoot,StringsearchTerm) {
19+
List<String>result =newArrayList<>();
20+
TrieNodenode =root;
21+
for (charc :searchTerm.toCharArray()) {
22+
if (node.children[c -'a'] ==null) {
23+
returnresult;
24+
}else {
25+
node =node.children[c -'a'];
26+
}
27+
}
28+
if (node.isWord) {
29+
result.add(searchTerm);
30+
}
31+
for (TrieNodechild :node.children) {
32+
if (child !=null) {
33+
List<String>thisResult =dfs(child,searchTerm,newArrayList<>());
34+
result.addAll(thisResult);
35+
if (result.size() >=3) {
36+
returnresult.subList(0,3);
37+
}
38+
}
39+
}
40+
returnresult;
41+
}
42+
43+
privateList<String>dfs(TrieNodenode,Stringsubstring,List<String>result) {
44+
if (node.isWord) {
45+
result.add(substring +node.c);
46+
if (result.size() >=3) {
47+
returnresult;
48+
}
49+
}
50+
for (TrieNodechild :node.children) {
51+
if (child !=null) {
52+
dfs(child,substring +node.c,result);
53+
}
54+
}
55+
returnresult;
56+
}
57+
58+
privateTrieNodebuildTrie(String[]products) {
59+
TrieNoderoot =newTrieNode(' ');
60+
for (Stringpro :products) {
61+
insert(pro,root);
62+
}
63+
returnroot;
64+
}
65+
66+
privatevoidinsert(Stringword,TrieNoderoot) {
67+
TrieNodenode =root;
68+
for (inti =0;i <word.length();i++) {
69+
charc =word.charAt(i);
70+
if (node.children[c -'a'] ==null) {
71+
node.children[c -'a'] =newTrieNode(c);
72+
}
73+
node =node.children[c -'a'];
74+
}
75+
node.isWord =true;
76+
}
77+
78+
classTrieNode {
79+
TrieNode[]children =newTrieNode[26];
80+
booleanisWord;
81+
charc;
82+
83+
publicTrieNode(charc) {
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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1268;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importjava.util.Arrays;
8+
importjava.util.List;
9+
10+
importstaticorg.junit.Assert.assertEquals;
11+
12+
publicclass_1268Test {
13+
privatestatic_1268.Solution1solution1;
14+
privatestaticList<List<String>>expected;
15+
privatestaticString[]products;
16+
17+
@BeforeClass
18+
publicstaticvoidsetup() {
19+
solution1 =new_1268.Solution1();
20+
}
21+
22+
@Test
23+
publicvoidtest1() {
24+
products =newString[]{"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)

[8]ページ先頭

©2009-2025 Movatter.jp