File tree 1 file changed +37
-0
lines changed
implement-trie-prefix-tree
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+ import java .util .Map ;
3
+
4
+ // Map으로 풀려버려서 당황..
5
+ // 이진트리? 어떤식으로 풀어야 할지 자료구조 정하고 다시 풀어보기..
6
+ class Trie {
7
+
8
+ Map <String , Boolean > tMap ;
9
+
10
+ public Trie () {
11
+ this .tMap = new HashMap <>();
12
+ }
13
+
14
+ public void insert (String word ) {
15
+ this .tMap .put (word , true );
16
+ }
17
+
18
+ public boolean search (String word ) {
19
+ return this .tMap .containsKey (word );
20
+ }
21
+
22
+ public boolean startsWith (String prefix ) {
23
+ for (String key : this .tMap .keySet ()) {
24
+ if (key .startsWith (prefix )) return true ;
25
+ }
26
+
27
+ return false ;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Your Trie object will be instantiated and called as such:
33
+ * Trie obj = new Trie();
34
+ * obj.insert(word);
35
+ * boolean param_2 = obj.search(word);
36
+ * boolean param_3 = obj.startsWith(prefix);
37
+ */
You can’t perform that action at this time.
0 commit comments