Design a data structure that supports the following two operations:
void addWord(word) bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z
or .
.
A .
means it can represent any one letter.
For example:
addWord("bad") addWord("dad") addWord("mad") search("pad") -> false search("bad") -> true search(".ad") -> true search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z
.
public class WordDictionary {
class TrieNode {
final static int maxnode = 40000;
final static int sigma_size = 26;
// Initialize your data structure here.
int ch[][] = new int[maxnode][sigma_size];
int val[] = new int[maxnode];
int sz = 1; // 结点总数
int idx(char c) {
return c - 'a';
} // 字符c的编号
// 插入字符串s,附加信息为v。注意v必须非0,因为0代表“本结点不是单词结点”
void insert(String s, int v) {
int u = 0, n = s.length();
for (int i = 0; i < n; i++) {
int c = idx(s.charAt(i));
if (ch[u][c] == 0) { // 结点不存在
//memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0; // 中间结点的附加信息为0
ch[u][c] = sz++; // 新建结点
}
u = ch[u][c]; // 往下走
}
val[u] = v;
}
int find(String s)//查找s,返回权值
{
int u = 0;
for (int i = 0; i < s.length(); i++) {
int c = idx(s.charAt(i));
if (ch[u][c] == 0) return -1;
u = ch[u][c];
}
return val[u];
}
int findRescure(String s, int u, int start) {
if (start == s.length()) return val[u];
int c = idx(s.charAt(start));
if (ch[u][c] == 0) return -1;
return findRescure(s, ch[u][c], start + 1);
}
int findWithDot(String s, int u, int start) {
if (start == s.length()) {
return val[u];
}
if (s.charAt(start) == '.') {
for (int j = 0; j < 26; j++) {
if (ch[u][j] == 0) continue;
int res = findWithDot(s, ch[u][j], start + 1);
if (res != -1 && res != 0)
return res;
}
} else {
int c = idx(s.charAt(start));
if (ch[u][c] == 0) {
return -1;
}
return findWithDot(s, ch[u][c], start + 1);
}
return -1;
}
public TrieNode() {
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
root.insert(word, 1);
}
// Returns if the word is in the trie.
public boolean search(String word) {
int res = root.findWithDot(word,0,0);
return res != -1 && res != 0;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
return root.find(prefix) != -1;
}
}
Trie trie = new Trie();
// Adds a word into the data structure.
public void addWord(String word) {
trie.insert(word);
}
// 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 trie.search(word);
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");