-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWordDictionary.java
More file actions
53 lines (49 loc) · 1.44 KB
/
WordDictionary.java
File metadata and controls
53 lines (49 loc) · 1.44 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
/*
class TrieNode{
TrieNode[] children = new TrieNode[26];
boolean isWordEnd;
}
* */
// Word Dictionary 就是Trie!
public class WordDictionary {
public TrieNode root;
/** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
TrieNode cur = root;
for(char c : word.toCharArray()){
TrieNode next = cur.children[c-'a'];
if(next == null){
next = new TrieNode();
cur.children[c-'a'] = next;
}
cur = next;
}
// 注意写在FOR循环外面!
cur.isWordEnd = 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 isMatch(root, word, 0);
}
public boolean isMatch(TrieNode root, String word, int level){
if(root == null){
return false;
}
if(level == word.length()){
return root.isWordEnd;
}
char c = word.charAt(level);
if(c == '?'){
for(TrieNode child : root.children){
if(isMatch(child, word, level+1)) return true;
}
return false;
}else{
return isMatch(root.children[c-'a'], word, level+1);
}
}
}