File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
implement-trie-prefix-tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ */
You can’t perform that action at this time.
0 commit comments