-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd and Search Word.java
157 lines (136 loc) · 4.33 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
public class WordDictionary {
private TrieNode root;
// initialize
public WordDictionary(){
this.root = new TrieNode();
}
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode node = root;
for(int i = 0; i < word.length(); i++){
node = node.addChildren(word.charAt(i));
}
node.setIsWord();
}
// 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, root, 0);
}
// Depth first search
// index == "." ,from start to check if the next trienode is valid
private boolean search(String word, TrieNode root, int index){
if(index == word.length()){
return root.isWord();
}
if(word.charAt(index) == '.'){
for(TrieNode child : root.getAllChildren()){
if(search(word, child, index + 1)){
return true;
}
}
}else{
TrieNode node = root.getOneChild(word.charAt(index));
if(node != null){
return search(word, node, index + 1);
}
}
return false;
}
}
class TrieNode {
private Map<Character, TrieNode> children;
private boolean isWord;
public TrieNode(){
this.children = new HashMap<Character, TrieNode>();
}
void setIsWord(){
isWord = true;
}
boolean isWord(){
return isWord;
}
// add children in if it is not in the trie
TrieNode addChildren(Character c){
if(!children.containsKey(c)){
children.put(c, new TrieNode());
}
return children.get(c);
}
TrieNode getOneChild(Character c){
return children.get(c);
}
List<TrieNode> getAllChildren(){
return new ArrayList<TrieNode>(children.values());
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
// solution 2
public class WordDictionary {
private trieNode root;
public WordDictionary(){
root = new trieNode(' ');
}
// Adds a word into the data structure.
public void addWord(String word) {
trieNode node = root;
if(word == null) return;
for(int i = 0; i < word.length(); i++){
char key = word.charAt(i);
trieNode child = node.children.get(key);
if(child == null){
trieNode newNode = new trieNode(key);
node.children.put(key, newNode);
node = newNode;
}else{
node = child;
}
if(i == word.length() - 1){
node.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 searchHelper(word, root);
}
// function 采用的是DFS,word往下走,root->root.children.get(key)
public boolean searchHelper(String word, trieNode root){
if(word == null || word.length() == 0){
return root.isword;
}
char c = word.charAt(0);
// normal situation
if(c != '.'){
if(root.children.containsKey(c)){
return searchHelper(word.substring(1), root.children.get(c));
}else{
return false;
}
// we check all the children values and apply with searchHelper
}else{
for(trieNode child : root.children.values()){
if(searchHelper(word.substring(1), child)){
return true;
}
}
return false;
}
}
}
class trieNode{
boolean isword = false;
char c;
HashMap<Character, trieNode> children = new HashMap<Character, trieNode>();
public trieNode(char c){
this.c = c;
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");