-
Notifications
You must be signed in to change notification settings - Fork 42
/
72--design-add-and-search-words-data-structure.java
55 lines (46 loc) · 1.47 KB
/
72--design-add-and-search-words-data-structure.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
class WordDictionary {
class Node {
HashMap<Character, Node> child;
boolean isEnd;
public Node () {
child = new HashMap<>();
isEnd = false;
}
}
Node root;
public WordDictionary() {
root = new Node();
}
public void addWord(String word) {
Node curr = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (!curr.child.containsKey(ch)) {
curr.child.put(ch, new Node());
}
curr = curr.child.get(ch);
}
curr.isEnd = true;
} // TC: O(n), SC: O(n)
public boolean search(String word) {
return searchWord(word, 0, root);
}
public boolean searchWord(String word, int index, Node curr) {
if (index == word.length()) return curr.isEnd;
if (word.charAt(index) == '.') {
for (Character ch : curr.child.keySet()) {
if (searchWord(word, index + 1, curr.child.get(ch))) return true;
}
return false;
} else {
if (curr.child.get(word.charAt(index)) == null) return false;
return searchWord(word, index + 1, curr.child.get(word.charAt(index)));
}
} // TC: O(n^2), SC: O(n)
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/