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