-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementTrie.java
More file actions
32 lines (28 loc) · 802 Bytes
/
ImplementTrie.java
File metadata and controls
32 lines (28 loc) · 802 Bytes
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
class Trie {
HashMap<String, Boolean> map;
public Trie() {
map = new HashMap<>();
}
public void insert(String word) {
if(!map.containsKey(word))
map.put(word,true);
}
public boolean search(String word) {
if(map.containsKey(word)) return true;
return false;
}
public boolean startsWith(String prefix) {
for(String str : map.keySet()){
if(prefix.length() <= str.length() && prefix.equals(str.substring(0,prefix.length())))
return true;
}
return false;
}
}
/**
* 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);
*/