-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImplementTrie.java
131 lines (116 loc) · 3.5 KB
/
ImplementTrie.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
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
131
// This method can fail if the second constraint is removed
/*
Runtime: 29 ms, faster than 94.63% of Java online submissions for Implement Trie (Prefix Tree).
Memory Usage: 49.9 MB, less than 51.07% of Java online submissions for Implement Trie (Prefix Tree).
*/
class ImplementTrie {
Node root = null;
class Node {
Node[] nodeCheck;
boolean leaf;
Node() {
this.nodeCheck = new Node[26];
this.leaf = false;
}
}
/** Initialize your data structure here. */
public ImplementTrie() {
// initialize root
root = new Node();
}
/** Inserts a word into the trie. */
public void insert(String word) {
Node tempNode = root;
for(char charValue: word.toCharArray()) {
Node[] tempArr = tempNode.nodeCheck;
if(tempArr[charValue - 'a'] == null) {
Node newNode = new Node();
tempArr[charValue - 'a'] = newNode;
}
tempNode = tempArr[charValue - 'a'];
}
tempNode.leaf = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
Node tempNode = root;
for(char charValue: word.toCharArray()) {
Node[] tempArr = tempNode.nodeCheck;
if(tempArr[charValue - 'a'] == null) {
return false;
}
tempNode = tempArr[charValue - 'a'];
}
return tempNode.leaf;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
Node tempNode = root;
for(char charValue: prefix.toCharArray()) {
Node[] tempArr = tempNode.nodeCheck;
if(tempArr[charValue - 'a'] == null) {
return false;
}
tempNode = tempArr[charValue - 'a'];
}
return true;
}
}
/*
class Trie {
class Node{
Map<Character, Node> children;
boolean endOfWord;
public Node() {
children = new HashMap<>();
endOfWord = false;
}
private Node root;
public Trie() {
root=new Node('\0');
}
public void insert(String word) {
Node current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
Node node = current.children.get(ch);
if (node == null) {
node = new Node();
current.children.put(ch, node);
}
current = node;
}
//mark the current nodes endOfWord as true
current.endOfWord = true;
}
public boolean search(String word) {
Node current=root;
for(int i=0;i<word.length();i++)
{
char ch=word.charAt(i);
Node node=current.children.get(ch);
if(node==null) return false;
}
if(current.endOfWord)
return true;
else return false;
}
public boolean startsWith(String prefix) {
Node current=root;
for(int i=0;i<word.length();i++)
{
char ch=word.charAt(i);
Node node=current.children.get(ch);
if(node==null) return false;
}
return true;
}
}
}*/
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/