Skip to content

Commit 3d90a93

Browse files
solves trie #208 in java
1 parent 6e51450 commit 3d90a93

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
| 205 | [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings) | [![Java](assets/java.png)](src/IsomorphicStrings.java) [![Python](assets/python.png)](python/isomorphic_strings.py) | |
169169
| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list) | [![Java](assets/java.png)](src/ReverseLinkedList.java) [![Python](assets/python.png)](python/reverse_linked_list.py) | |
170170
| 207 | [Course Schedule](https://leetcode.com/problems/course-schedule) | [![Java](assets/java.png)](src/CourseSchedule.java) | |
171-
| 208 | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree) | | |
171+
| 208 | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree) | [![Java](assets/java.png)](src/Trie.java) | |
172172
| 209 | [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum) | | |
173173
| 210 | [Course Schedule II](https://leetcode.com/problems/course-schedule-ii) | | |
174174
| 211 | [Design Add and Search Words Data Structure](https://leetcode.com/problems/design-add-and-search-words-data-structure) | | |

Diff for: src/HelloWorld.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
public class HelloWorld {
22
public static void main(String[] args) {
3-
System.out.println("Hello world");
3+
Trie trie = new Trie();
4+
trie.insert("apple");
5+
// System.out.println(trie.search("apple"));
6+
// System.out.println(trie.search("app"));
7+
System.out.println(trie.startsWith("app")); // true
8+
trie.insert("app");
9+
// System.out.println(trie.search("app"));
410
}
511
}

Diff for: src/Trie.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// https://leetcode.com/problems/implement-trie-prefix-tree
2+
// insertion
3+
// T: O(n)
4+
// S: O(n)
5+
// search
6+
// T: O(n)
7+
// S: O(n)
8+
// starts with
9+
// T: O(n)
10+
// S: O(n)
11+
12+
public class Trie {
13+
private final Trie[] alphabet = new Trie[26];
14+
private boolean isWordEnd = false;
15+
16+
public Trie() { }
17+
18+
public void insert(String word) {
19+
insert(word, 0);
20+
}
21+
22+
private void insert(String word, int index) {
23+
if (index == word.length()) {
24+
this.isWordEnd = true;
25+
return;
26+
}
27+
int charIndex = toIndex(word.charAt(index));
28+
if (alphabet[charIndex] == null) {
29+
alphabet[charIndex] = new Trie();
30+
}
31+
alphabet[charIndex].insert(word, index + 1);
32+
}
33+
34+
public boolean search(String word) {
35+
return search(word, 0);
36+
}
37+
38+
public boolean search(String word, int index) {
39+
if (index == word.length()) return isWordEnd;
40+
int charIndex = toIndex(word.charAt(index));
41+
if (alphabet[charIndex] == null) return false;
42+
return alphabet[charIndex].search(word, index + 1);
43+
}
44+
45+
public boolean startsWith(String prefix) {
46+
return startsWith(prefix, 0);
47+
}
48+
49+
public boolean startsWith(String prefix, int index) {
50+
if (index == prefix.length()) return true;
51+
int charIndex = toIndex(prefix.charAt(index));
52+
if (alphabet[charIndex] == null) return false;
53+
return alphabet[charIndex].startsWith(prefix, index + 1);
54+
}
55+
56+
private int toIndex(char c) {
57+
return c - 'a';
58+
}
59+
}

0 commit comments

Comments
 (0)