Skip to content

Commit 75217ae

Browse files
committed
Runtime: 67 ms (Top 53.89%) | Memory: 70 MB (Top 22.15%)
1 parent 45efc18 commit 75217ae

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

scripts/algorithms/I/Implement Trie (Prefix Tree)/Implement Trie (Prefix Tree).java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 67 ms (Top 53.89%) | Memory: 70 MB (Top 22.15%)
12
class Trie {
23
private class Node{
34
char character;
@@ -13,31 +14,31 @@ public Trie() {
1314
root = new Node('\0');
1415
map = new HashMap<>();
1516
}
16-
17+
1718
public void insert(String word) {
1819
Node temp = root;
1920
for(char c : word.toCharArray()){
2021
int index = c-'a';
2122
if(temp.sub[index] == null)
2223
temp.sub[index] = new Node(c);
23-
24+
2425
temp = temp.sub[index];
2526
}
2627
map.put(word, true);
2728
}
28-
29+
2930
public boolean search(String word) {
3031
return map.containsKey(word);
3132
}
32-
33+
3334
public boolean startsWith(String prefix) {
3435
Node temp = root;
3536
for(char c : prefix.toCharArray()){
3637
int index = c-'a';
3738
if(temp.sub[index] == null)
3839
return false;
39-
temp = temp.sub[index];
40+
temp = temp.sub[index];
4041
}
4142
return true;
4243
}
43-
}
44+
}

0 commit comments

Comments
 (0)