-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.java
More file actions
78 lines (65 loc) · 2.2 KB
/
Trie.java
File metadata and controls
78 lines (65 loc) · 2.2 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
import java.util.*;
import java.util.function.Predicate;
/*
* https://leetcode.com/problems/implement-trie-prefix-tree/
*/
public class Trie {
private final TreeNode root;
/** Initialize your data structure here. */
public Trie() {
root = new TreeNode(' ');
}
/** Inserts a word into the trie. */
public void insert(String word) {
insert(root, word + '\n', 0);
}
private void insert(TreeNode node, String word, int index) {
if (index == word.length()) {
return;
}
TreeNode child = node.children.computeIfAbsent(word.charAt(index), TreeNode::new);
insert(child, word, index + 1);
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
return search(root, ' ' + word, 0, treeNode -> treeNode.children.containsKey('\n'));
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
return search(root, ' ' + prefix, 0, treeNode -> true);
}
private boolean search(TreeNode node, String word, int index, Predicate<TreeNode> finalCheck) {
if (node == null) {
return false;
}
if (index == word.length() - 1) {
return finalCheck.test(node);
}
char c = word.charAt(index);
return node.val == c && search(node.children.get(word.charAt(index + 1)), word, index + 1, finalCheck);
}
static class TreeNode {
char val;
Map<Character, TreeNode> children;
public TreeNode(char val) {
this.val = val;
this.children = new HashMap<>();
}
public TreeNode(char val, Map<Character, TreeNode> children) {
this.val = val;
this.children = children;
}
@Override
public String toString() {
return String.valueOf(val);
}
}
public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("apple");
System.out.println(trie.startsWith("app"));
System.out.println(trie.search("app"));
trie.insert("app");
System.out.println(trie.search("app"));
}
}