- Add and Search Word - Data structure design
Design a data structure that supports the following two operations: addWord(word) and 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.
Example
addWord(“bad”)
addWord(“dad”)
addWord(“mad”)
search(“pad”) // return false
search(“bad”) // return true
search(“.ad”) // return true
search(“b…”) // return true
Notice
You may assume that all words are consist of lowercase letters a-z.
思路:用Trie。参考了网上的模板。
代码如下:
struct TrieNode {
bool isWord;
TrieNode *children[26];
TrieNode() : isWord(false) {
for (int i = 0; i < 26; ++i) children[i] = NULL;
}
};
class WordDictionary {
public:
WordDictionary() {
root = new TrieNode();
}
/*
* @param word: Adds a word into the data structure.
* @return: nothing
*/
void addWord(string &word) {
TrieNode * cur = root;
int len = word.size();
for (int i = 0; i < len; ++i) {
int index = word[i] - 'a';
if (!cur->children[index]) cur->children[index] = new TrieNode();
cur = cur->children[index];
}
cur->isWord = true;
}
/*
* @param word: A word could contain the dot character '.' to represent any one letter.
* @return: if the word is in the data structure.
*/
bool search(string &word) {
int n = word.size();
return helper(word, n, 0, root);
}
private:
TrieNode * root;
bool helper(string & word, int n, int pos, TrieNode * cur) {
if (!cur) return false;
if (pos == n) return cur->isWord;
if (word[pos] == '.') {
for (int i = 0; i < 26; ++i) {
if (helper(word, n, pos + 1, cur->children[i])) return true;
}
} else {
int index = word[pos] - 'a';
if (cur->children[index]) {
if (helper(word, n, pos + 1, cur->children[index])) return true;
}
}
return false;
}
};
二刷:
struct TrieNode {
bool isWord;
vector<TrieNode *> children;
TrieNode() : isWord(false) {
children.resize(26, NULL);
}
};
class WordDictionary {
public:
WordDictionary() {
root = new TrieNode();
}
/*
* @param word: Adds a word into the data structure.
* @return: nothing
*/
void addWord(string &word) {
int n = word.size();
TrieNode *node = root;
for (int i = 0; i < n; i++) {
if (node->children[word[i] - 'a'] == NULL) node->children[word[i] - 'a'] = new TrieNode();
node = node->children[word[i] - 'a'];
}
node->isWord = true;
}
/*
* @param word: A word could contain the dot character '.' to represent any one letter.
* @return: if the word is in the data structure.
*/
bool search(string &word) {
int n = word.size();
return helper(word, n, 0, root);
}
private:
bool helper(string &word, int n, int index, TrieNode *root) {
if (!root) return false;
if (index == n) {
if (root->isWord) return true;
else return false;
}
if (word[index] == '.') {
for (int i = 0; i < 26; i++) {
if (helper(word, n, index + 1, root->children[i])) return true;
}
return false;
} else {
return helper(word, n, index + 1, root->children[word[index] - 'a']);
}
}
TrieNode *root;
};
helper()写成这样也可以
bool helper(string &word, int pos, TrieNode *root) {
if (pos == word.size()) return root->isWord;
char c = word[pos];
if (c == '.') {
for (int i = 0; i < 26; i++) {
if (!root->children[i]) continue;
if (helper(word, pos + 1, root->children[i])) return true;
}
} else {
if (!root->children[c - 'a']) return false;
return helper(word, pos + 1, root->children[c - 'a']);
}
return false;
}
注意:下面的做法不对。也就是说addWord里面可以有for (int i = 0; i < n; i++)这重循环,但有了‘.’这样的通配符后,search()里面不能有for (int i = 0; i < n; i++)这重循环。
以word=“…a.“为例。
一开始的helper()里面,word=”…a.”, for 循环中,i=0时,会去掉第一个’.‘,然后根据j=0…25中可行的解调用helper(“…a.”, 新root),然后里面helper()又会调用helper(“.a.”, 新新root); 等等。
i=1时,会去掉第二个’.',然后根据j=0…25中可行的解调用helper(“…a.”, 新root),如此这般。首先我们可以看到有大量的重复操作。
另外一个更严重的问题是它可能会过早的返回false,而实际上是有可行解的。
例如,Trie里面已经有"runner"和"addee",search “…e.”。
当search完"addee"后,发现没有isWord,返回false。而此时search “runner”的工作还没有结束。
其实for()循环和递归本来就是做同样的事情,结果搞到一起,把事情搞砸了。
bool search(string &word) {
cout << " search " << word << endl;
return helper(word, root);
}
private:
TrieNode *root = NULL;
bool helper(string word, TrieNode* root) {
int n = word.size();
TrieNode *node = root;
if (word.size() == 0) return root->isWord;
for (int i = 0; i < n; i++) {
char c = word[i];
if (c == '.') {
for (int j = 0; j < 26; j++) {
if (!node->children[j]) continue;
node = node->children[j];
bool res = helper(word.substr(i + 1), node);
if (res) return true;
}
} else {
int index = c - 'a';
if (!node->children[index]) return false;
node = node->children[index];
return helper(word.substr(i + 1), node);
}
}
return false;
}