-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImplementTriePrefixTree.java
More file actions
71 lines (58 loc) · 2.05 KB
/
Copy pathImplementTriePrefixTree.java
File metadata and controls
71 lines (58 loc) · 2.05 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
// Implement a trie with insert, search, and startsWith methods.
// See: https://leetcode.com/problems/implement-trie-prefix-tree/
// See: https://leetcode.com/problems/implement-trie-prefix-tree/discuss/593601/Java-Concise-Solution
package leetcode.design;
import java.util.HashMap;
import java.util.Map;
public class ImplementTriePrefixTree {
class Trie {
final class Node {
final Map<Character, Node> map = new HashMap<>();
boolean isEnd = false;
Node() {
}
}
final Node root;
/** Initialize your data structure here. */
public Trie() {
this.root = new Node();
}
/** Inserts a word into the trie. */
public void insert(String word) {
Node curr = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
curr.map.computeIfAbsent(ch, k -> new Node());
curr = curr.map.get(ch);
if (i == word.length() - 1)
curr.isEnd = true;
}
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
return find(word, false);
}
/**
* Returns if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String prefix) {
return find(prefix, true);
}
private boolean find(String str, boolean isPrefix) {
Node curr = root;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (!curr.map.containsKey(ch))
return false;
curr = curr.map.get(ch);
}
return isPrefix ? true : curr.isEnd;
}
}
public static void main(String[] args) {
Trie trie = new ImplementTriePrefixTree().new Trie();
trie.insert("apple");
System.out.println(trie.search("apple"));
System.out.println(trie.search("app"));
}
}