We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d74ceb7 commit 6609876Copy full SHA for 6609876
implement-trie-prefix-tree/donghyeon95.java
@@ -0,0 +1,29 @@
1
+import java.util.HashMap;
2
+
3
+class Trie {
4
+ HashMap<String, Boolean> trie;
5
+ public Trie() {
6
+ trie = new HashMap<>();
7
+ }
8
9
+ public void insert(String word) {
10
+ StringBuilder sb = new StringBuilder();
11
+ for (char c: word.toCharArray()) {
12
+ sb.append(c);
13
+ trie.putIfAbsent(sb.toString(), false);
14
15
+ trie.put(sb.toString(), true);
16
17
18
+ public boolean search(String word) {
19
+ return trie.getOrDefault(word, false);
20
21
22
+ public boolean startsWith(String prefix) {
23
+ return trie.containsKey(prefix);
24
25
+}
26
27
28
29
0 commit comments