-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementTriePrefixTree.java
More file actions
130 lines (114 loc) · 3.1 KB
/
ImplementTriePrefixTree.java
File metadata and controls
130 lines (114 loc) · 3.1 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package leetcode;
/**
* ImplementTriePrefixTree
* https://leetcode-cn.com/problems/implement-trie-prefix-tree/
* 208. 实现 Trie (前缀树)
* https://leetcode-cn.com/problems/implement-trie-prefix-tree/solution/shi-xian-shu-jie-gou-by-oshdyr-4qgt/
*
* @since 2021-04-14
*/
public class ImplementTriePrefixTree {
public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("apple");
System.out.println(trie.search("apple")); // 返回 True
System.out.println(trie.search("app")); // 返回 False
System.out.println(trie.startsWith("app")); // 返回 True
trie.insert("app");
System.out.println(trie.search("app")); // 返回 True
}
}
class Trie {
TrieNode head;
/**
* Initialize your data structure here.
*/
public Trie() {
head = new TrieNode();
}
/**
* Inserts a word into the trie.
*/
public void insert(String word) {
if (word == null || word.length() < 1) {
return;
}
TrieNode next = head;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
TrieNode curr = next.get(c);
if (curr == null) {
next.set(c, new TrieNode());
curr = next.get(c);
}
next = curr;
}
next.setLeaf(true);
}
/**
* Returns if the word is in the trie.
*/
public boolean search(String word) {
if (word == null || word.length() < 1) {
return false;
}
TrieNode next = head;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
TrieNode curr = next.get(c);
if (curr == null) {
return false;
}
next = curr;
}
return next.isLeaf();
}
/**
* Returns if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String prefix) {
if (prefix == null || prefix.length() < 1) {
return false;
}
TrieNode next = head;
for (int i = 0; i < prefix.length(); i++) {
char c = prefix.charAt(i);
TrieNode curr = next.get(c);
if (curr == null) {
return false;
}
next = curr;
}
return true;
}
}
class TrieNode {
private static final int SIZE = 26;
private TrieNode[] children;
private boolean isLeaf;
public TrieNode() {
children = new TrieNode[SIZE];
isLeaf = false;
}
public boolean set(char c, TrieNode node) {
int idx = c - 'a';
if (idx < 0 || idx >= SIZE) {
return false;
}
children[idx] = node;
return true;
}
public TrieNode get(char c) {
int idx = c - 'a';
if (idx < 0 || idx >= SIZE) {
return null;
}
return children[idx];
}
public boolean isLeaf() {
return isLeaf;
}
public void setLeaf(boolean leaf) {
isLeaf = leaf;
}
}