Skip to content

Commit bb07450

Browse files
author
jinvicky
committed
implement trie prefix tree solution
1 parent 18bd398 commit bb07450

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* brute-force로 먼저 접근한 423ms의 최악의 성능 코드.
6+
*/
7+
class Trie {
8+
9+
private List<String> list;
10+
11+
public Trie() {
12+
this.list = new ArrayList<>();
13+
}
14+
15+
public void insert(String word) {
16+
this.list.add(word);
17+
}
18+
19+
public boolean search(String word) {
20+
for(String s : list) {
21+
if(s.equals(word)) return true;
22+
}
23+
return false;
24+
}
25+
26+
public boolean startsWith(String prefix) {
27+
for(String s : list) {
28+
if(s.startsWith(prefix)) return true;
29+
}
30+
return false;
31+
}
32+
}
33+
34+
/**
35+
* Your Trie object will be instantiated and called as such:
36+
* Trie obj = new Trie();
37+
* obj.insert(word);
38+
* boolean param_2 = obj.search(word);
39+
* boolean param_3 = obj.startsWith(prefix);
40+
*/

0 commit comments

Comments
 (0)