# LeetCode-211 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.

E

```addWord("bad")
search("b..") -> true```

```class TrieNode {
public:
//该节点代表的字母是否是某个单词的最后一个字母
bool word;
TrieNode* children[26];
TrieNode() {
word = false;
memset(children, NULL, sizeof(children));
}
};

class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() {

}

//建立一棵树
/** Adds a word into the data structure. */
TrieNode* node = root;
for (char c : word) {
if (!node -> children[c - ‘a‘]) {
node -> children[c - ‘a‘] = new TrieNode();
}
node = node -> children[c - ‘a‘];
}
node -> word = true;
}

/** Returns if the word is in the data structure. A word could contain the dot character ‘.‘ to represent any one letter. */
bool search(string word) {
return search(word.c_str(), root);
}
private:
TrieNode* root = new TrieNode();

bool search(const char* word, TrieNode* node) {
for (int i = 0; word[i] && node; i++) {
//若搜索的单词该位置不是‘.’，则直接进行比较，否则对所有子结点进行遍历搜索
if (word[i] != ‘.‘) {
node = node -> children[word[i] - ‘a‘];
} else {
TrieNode* tmp = node;
for (int j = 0; j < 26; j++) {
node = tmp -> children[j];
if (search(word + i + 1, node)) {
return true;
}
}
}
}
return node && node -> word;
}
};```

LeetCode-211 Add and Search Word - Data structure design

(0)
(0)