-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordDictionary.java
55 lines (52 loc) · 1.58 KB
/
WordDictionary.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
// 211. Design Add and Search Words Data Structure
class TrieNode{
boolean isEnd;
Map<Character, TrieNode> childNodes;
TrieNode(){
isEnd = false;
childNodes = new HashMap<>();
}
}
class WordDictionary {
TrieNode root;
public WordDictionary() {
root = new TrieNode();
}
public void addWord(String word) {
TrieNode temp = root;
for(Character ch: word.toCharArray()){
if(!temp.childNodes.containsKey(ch))
temp.childNodes.put(ch, new TrieNode());
temp = temp.childNodes.get(ch);
}
temp.isEnd = true;
}
public boolean search(String word, TrieNode base) {
TrieNode temp = base;
for(int i = 0; i < word.length(); i++){
Character ch = word.charAt(i);
if(ch.equals('.')){
boolean found = false;
for(Character key: temp.childNodes.keySet()){
TrieNode node = temp.childNodes.get(key);
if(search(word.substring(i+1), node))
return true;
}
return false;
}
if(!temp.childNodes.containsKey(ch))
return false;
temp = temp.childNodes.get(ch);
}
return temp.isEnd == true;
}
public boolean search(String word) {
return search(word, root);
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/