-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignAddSearchWordsDataStructure.java
More file actions
71 lines (61 loc) · 2.28 KB
/
DesignAddSearchWordsDataStructure.java
File metadata and controls
71 lines (61 loc) · 2.28 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
/** prefix tree str for given eg.
TrieNode(TN) structure b
_______________________________ m
| HashMap | | d
| key | TN(val)| | |
| | | isEnd | /|\
| | | | / | \
|_______________|______________| / | \
a a a
/ | \
d d d
/ | \
isEnd=true | isEnd=true
isEnd=true
*/
class WordDictionary {
private TrieNode root;
public WordDictionary() {
root = new TrieNode();
}
public void addWord(String word) {
TrieNode node = root;
for(char c : word.toCharArray()){
node.child.putIfAbsent(c, new TrieNode());
node = node.child.get(c);
}
node.isEnd = true;
}
public boolean search(String word) {
return searchTree(word, 0, root);
}
public boolean searchTree(String word, int index, TrieNode node){
if(node == null) return false;
if(index == word.length()) return node.isEnd;
char c = word.charAt(index);
if(c == '.'){
for(TrieNode child : node.child.values()){
if(searchTree(word, index+1, child))
return true;
}
return false;
}
else
return searchTree(word, index + 1, node.child.get(c)); //note if key not in hashmap it will return null
}
//using a prefix tree(trie) for fast search and insert
class TrieNode {
Map<Character, TrieNode> child;
boolean isEnd;
TrieNode(){
child = new HashMap<>();
isEnd = false;
}
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/