题目描述:

Design a data structure that supports the following two operations:

1
2
3
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:

1
2
3
4
5
6
7
8
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.

直接用前缀树来实现。

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class TrieNode {
public:
char val;
vector<TrieNode*> children;
TrieNode* parent;
bool wordEnd = false;
TrieNode(char v = 0, TrieNode *p = nullptr) : val(v), parent(p) {}
};

class Trie {
public:
TrieNode* root;
Trie() {
root = new TrieNode();
}

// Inserts a word into the trie.
void insert(string word) {
insertToNode(word, root);
}

void insertToNode(string word, TrieNode* node) {
if (word.empty()) {
node->wordEnd = true;
return;
}
for (int i = 0; i < node->children.size(); i++) {
if (word[0] == node->children[i]->val) {
return insertToNode(word.substr(1), node->children[i]);
}
}
TrieNode *p = new TrieNode(word[0], node);
node->children.push_back(p);
for (int i = 1; i < word.size(); i++) {
TrieNode *tmp = new TrieNode(word[i], p);
p->children.push_back(tmp);
p = tmp;
}
p->wordEnd = true;
}

// Returns if the word is in the trie.
bool search(string word, TrieNode *node = nullptr) {
int pos = 0;
TrieNode *p = (node ? node : root);
if (word.empty()) {
if (p->wordEnd)
return true;
else
return false;
}
for (; pos < word.size(); pos++) {
if (word[pos] == '.') {
if (p->children.empty() && word.size() > 1)
return false;
for (int i = 0; i < p->children.size(); i++) {
if (search(word.substr(pos + 1), p->children[i]))
return true;
}
return false;
}
else {
bool flag = false;
for (int i = 0; i < p->children.size(); i++) {
if (word[pos] == p->children[i]->val) {
p = p->children[i];
flag = true;
break;
}
}
if (flag == false) {
return false;
}
}
}
if (pos == word.size() && p->wordEnd) return true;
else return false;
}

// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
int pos = 0;
TrieNode *p = root;
for (; pos < prefix.size(); pos++) {
bool flag = false;
for (int i = 0; i < p->children.size(); i++) {
if (prefix[pos] == p->children[i]->val) {
p = p->children[i];
flag = true;
break;
}
}
if (flag == false) {
return false;
}
}
return true;
}

};

class WordDictionary {
public:
Trie trie;

// Adds a word into the data structure.
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.
bool search(string word) {
return trie.search(word);
}
};