原题网址:https://leetcode.com/problems/add-and-search-word-data-structure-design/
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.
You should be familiar with how a Trie works. If not, please work on this problem:
Implement Trie (Prefix Tree) first.
方法一:前缀树+广度优先搜索。
public class WordDictionary {
private TrieNode root = new TrieNode();
// Adds a word into the data structure.
public void addWord(String word) {
char[] wa = word.toCharArray();
TrieNode node = root;
for(int i=0; i<wa.length; i++) node = node.append(wa[i]);
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) {
if (word == null) return false;
char[] wa = word.toCharArray();
LinkedList<State> queue = new LinkedList<>();
queue.add(new State(root, 0));
while (!queue.isEmpty()) {
State state = queue.remove();
if (state.pos < wa.length) {
if (wa[state.pos] == '.') {
for(int i=0; i<26; i++) {
if (state.node.nexts[i] != null) {
queue.add(new State(state.node.nexts[i], state.pos+1));
}
}
} else if (state.node.nexts[wa[state.pos]-'a'] != null) {
queue.add(new State(state.node.nexts[wa[state.pos]-'a'], state.pos+1));
}
} else if (state.node.isWord) return true;
}
return false;
}
}
class TrieNode {
boolean isWord;
TrieNode[] nexts = new TrieNode[26];
TrieNode append(char ch) {
if (nexts[ch-'a']!=null) return nexts[ch-'a'];
nexts[ch-'a'] = new TrieNode();
return nexts[ch-'a'];
}
}
class State {
TrieNode node;
int pos;
State(TrieNode node, int pos) {
this.node = node;
this.pos = pos;
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
方法二:前缀树+深度优先搜索。
public class WordDictionary {
private TrieNode root = new TrieNode();
private boolean find(char[] wa, int pos, TrieNode node) {
if (pos == wa.length) return node.isWord;
if (wa[pos] == '.') {
for(int i=0; i<26; i++) {
if (node.nexts[i] != null && find(wa, pos+1, node.nexts[i])) return true;
}
} else return node.nexts[wa[pos]-'a'] != null && find(wa, pos+1, node.nexts[wa[pos]-'a']);
return false;
}
// Adds a word into the data structure.
public void addWord(String word) {
char[] wa = word.toCharArray();
TrieNode node = root;
for(int i=0; i<wa.length; i++) node = node.append(wa[i]);
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) {
if (word == null) return false;
char[] wa = word.toCharArray();
return find(wa, 0, root);
}
}
class TrieNode {
boolean isWord;
TrieNode[] nexts = new TrieNode[26];
TrieNode append(char ch) {
if (nexts[ch-'a']!=null) return nexts[ch-'a'];
nexts[ch-'a'] = new TrieNode();
return nexts[ch-'a'];
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");


被折叠的 条评论
为什么被折叠?



