-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path211.java
76 lines (72 loc) · 2.2 KB
/
211.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class WordDictionary {
class TreeNode{
boolean isWord;
char val;
Map<Character,TreeNode> map;
TreeNode(){
map = new HashMap<>();
isWord = false;
}
TreeNode(char c){
map = new HashMap<>();
isWord = false;
val = c;
}
}
TreeNode root;
/** Initialize your data structure here. */
public WordDictionary() {
root = new TreeNode();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
TreeNode temp = root;
for(int i=0;i<word.length();i++){
char c = word.charAt(i);
if(temp.map.containsKey(c)){
temp = temp.map.get(c);
}else{
temp.map.put(c,new TreeNode(c));
temp = temp.map.get(c);
}
}
temp.isWord=true;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) {
return search(word,0,root);
}
private boolean search(String word,int i,TreeNode cur){
if(i>=word.length()) return false;
char c = word.charAt(i);
if(i==word.length()-1){
if(c!='.'){
if(cur.map.containsKey(c)&&cur.map.get(c).isWord) return true;
return false;
}else{
for(Map.Entry<Character,TreeNode> entry : cur.map.entrySet()){
if(entry.getValue().isWord) return true;
}
return false;
}
}
if(c!='.'){
if(cur.map.containsKey(c)){
return search(word,i+1,cur.map.get(c));
}else{
return false;
}
}else{
for(Map.Entry<Character,TreeNode> entry : cur.map.entrySet()){
if(search(word,i+1,entry.getValue())) return true;
}
return false;
}
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/