Skip to content

Commit 1cd4d25

Browse files
authored
Update Trie.java
1 parent eae2b4e commit 1cd4d25

File tree

1 file changed

+41
-78
lines changed
  • src/main/java/g0201_0300/s0208_implement_trie_prefix_tree

1 file changed

+41
-78
lines changed

src/main/java/g0201_0300/s0208_implement_trie_prefix_tree/Trie.java

+41-78
Original file line numberDiff line numberDiff line change
@@ -3,104 +3,67 @@
33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Design #Trie
44
// #Level_2_Day_16_Design #Udemy_Trie_and_Heap
55
// #Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N)
6-
// #2024_11_15_Time_30_ms_(99.78%)_Space_55.1_MB_(72.51%)
7-
8-
import java.util.Arrays;
6+
// #2024_11_15_Time_32_ms_(95.05%)_Space_54.9_MB_(91.16%)
97

108
@SuppressWarnings("java:S1104")
119
public class Trie {
12-
private boolean ans = false;
13-
private final TrieNode[] trees = new TrieNode[26];
10+
private final TrieNode root;
11+
private boolean startWith;
1412

15-
TrieNode mapWordToTree(TrieNode t, String word, int i) {
16-
char m = word.charAt(i);
17-
boolean found = false;
18-
TrieNode a = t.nexts[m - 'a'];
19-
if (a != null) {
20-
if (i != word.length() - 1) {
21-
mapWordToTree(a, word, i + 1);
22-
} else {
23-
a.end = true;
24-
}
25-
found = true;
26-
}
27-
if (!found) {
28-
TrieNode prev = t;
29-
for (int j = i; j < word.length(); j++) {
30-
TrieNode temp = new TrieNode(word.charAt(j));
31-
prev.nexts[word.charAt(j) - 'a'] = temp;
32-
prev = temp;
33-
}
34-
prev.end = true;
13+
private static class TrieNode {
14+
// Initialize your data structure here.
15+
public TrieNode[] children;
16+
public boolean isWord;
17+
18+
public TrieNode() {
19+
children = new TrieNode[26];
3520
}
36-
return t;
3721
}
3822

23+
public Trie() {
24+
root = new TrieNode();
25+
}
26+
27+
// Inserts a word into the trie.
3928
public void insert(String word) {
40-
char a = word.charAt(0);
41-
if (trees[a - 'a'] == null) {
42-
TrieNode t = new TrieNode(a);
43-
trees[a - 'a'] = t;
44-
}
45-
if (1 == word.length()) {
46-
trees[a - 'a'].end = true;
47-
return;
48-
}
49-
trees[a - 'a'] = mapWordToTree(trees[a - 'a'], word, 1);
29+
insert(word, root, 0);
5030
}
5131

52-
public boolean searchWordInTree(TrieNode t, String word, int i) {
53-
char a = word.charAt(i);
54-
TrieNode m = t.nexts[a - 'a'];
55-
if (m != null) {
56-
if (i == word.length() - 1) {
57-
ans = true;
58-
return m.end;
59-
}
60-
return searchWordInTree(m, word, i + 1);
32+
private void insert(String word, TrieNode root, int idx) {
33+
if (idx == word.length()) {
34+
root.isWord = true;
35+
return;
36+
}
37+
int index = word.charAt(idx) - 'a';
38+
if (root.children[index] == null) {
39+
root.children[index] = new TrieNode();
6140
}
62-
return false;
41+
insert(word, root.children[index], idx + 1);
6342
}
6443

44+
// Returns if the word is in the trie.
6545
public boolean search(String word) {
66-
char a = word.charAt(0);
67-
if (trees[a - 'a'] == null) {
68-
return false;
69-
} else {
70-
if (1 == word.length()) {
71-
return trees[a - 'a'].end;
72-
}
73-
return searchWordInTree(trees[a - 'a'], word, 1);
74-
}
46+
return search(word, root, 0);
7547
}
7648

77-
public boolean startsWith(String prefix) {
78-
char a = prefix.charAt(0);
79-
ans = false;
80-
if (trees[a - 'a'] == null) {
49+
private boolean search(String word, TrieNode root, int idx) {
50+
if (idx == word.length()) {
51+
startWith = true;
52+
return root.isWord;
53+
}
54+
int index = word.charAt(idx) - 'a';
55+
if (root.children[index] == null) {
56+
startWith = false;
8157
return false;
82-
} else {
83-
if (1 == prefix.length()) {
84-
return true;
85-
}
86-
searchWordInTree(trees[a - 'a'], prefix, 1);
8758
}
88-
return ans;
59+
return search(word, root.children[index], idx + 1);
8960
}
9061

91-
static class TrieNode {
92-
char val;
93-
boolean end = false;
94-
TrieNode[] nexts = new TrieNode[26];
95-
96-
TrieNode(char val) {
97-
this.val = val;
98-
}
99-
100-
@Override
101-
public String toString() {
102-
return val + " " + Arrays.toString(nexts) + " " + end;
103-
}
62+
// Returns if there is any word in the trie
63+
// that starts with the given prefix.
64+
public boolean startsWith(String prefix) {
65+
search(prefix);
66+
return startWith;
10467
}
10568
}
10669

0 commit comments

Comments
 (0)