-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAdd and Search Word.java
59 lines (55 loc) · 1.75 KB
/
Add and Search Word.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
public class WordDictionary {
private static class TrieNode {
Map<Character, TrieNode> children = new TreeMap<Character, TrieNode>();
boolean isEnd = false;
public TrieNode() {
}
}
/*
* @param word: Adds a word into the data structure.
* @return: nothing
*/
private TrieNode root = new TrieNode();
public void addWord(String word) {
// write your code here
TrieNode cur = root;
for (char c : word.toCharArray()) {
if (!cur.children.containsKey(c)) {
cur.children.put(c, new TrieNode());
}
cur = cur.children.get(c);
}
cur.isEnd = true;
}
/*
* @param word: A word could contain the dot character '.' to represent any one letter.
* @return: if the word is in the data structure.
*/
public boolean search(String word) {
// write your code here
return search(root, word, 0);
}
private boolean search(TrieNode cur, String word, int index) {
if (index == word.length()) {
return cur.isEnd;
}
char c = word.charAt(index);
if (c == '.') {
for (TrieNode next : cur.children.values()) {
if (search(next, word, index + 1)) {
return true;
}
}
return false;
} else {
if (!cur.children.containsKey(c)) {
return false;
}
return search(cur.children.get(c), word, index + 1);
}
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");