Skip to content

Commit 304eb2b

Browse files
committed
211 Add and Search Word - Data structure design
1 parent f336949 commit 304eb2b

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

algorithms/AddandSearchWord/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Add and Search Word - Data structure design
2+
3+
Design a data structure that supports the following two operations:
4+
5+
```
6+
void addWord(word)
7+
bool search(word)
8+
```
9+
10+
`search(word)` can search a literal word or a regular expression string containing only letters a-z or `.`.
11+
12+
A `.` means it can represent any one letter.
13+
14+
For example:
15+
16+
```
17+
addWord("bad")
18+
addWord("dad")
19+
addWord("mad")
20+
search("pad") -> false
21+
search("bad") -> true
22+
search(".ad") -> true
23+
search("b..") -> true
24+
```
25+
26+
Note:
27+
You may assume that all words are consist of lowercase letters `a-z`.
28+
29+
click to show hint.
30+
31+
You should be familiar with how a Trie works. If not, please work on this problem: [Implement Trie (Prefix Tree)](../ImplementTrie) first.
32+
33+
## Solution
34+
35+
Trie树的扩展,如果搜索字符,和Trie树一样,若为".", 则DFS之
36+
37+
```cpp
38+
bool search(TrieNode *p, const char *target) const {
39+
if (p == nullptr)
40+
return false;
41+
int len = strlen(target);
42+
if (target == nullptr || len == 0) { // 到底单词尾部
43+
return p->exist;
44+
}
45+
char c = *target;
46+
if (c != '.') { // 字符不是"."
47+
int index = c - 'a';
48+
return search(p->children[index], target + 1);
49+
} else { // 字符是".", DFS
50+
for (int i = 0; i < TrieNode::SPACE_SIZE; ++i) {
51+
if (search(p->children[i], target + 1))
52+
return true;
53+
}
54+
return false;
55+
}
56+
}
57+
```
58+
59+
搜索单词`word`, 只需要调用`search(root, word.c_str())`即可.
60+
61+
## 扩展
62+
63+
[Implement Trie (Prefix Tree)](../ImplementTrie): Trie树实现

algorithms/AddandSearchWord/trie.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <cstdio>
2+
#include <string>
3+
#include <cstdlib>
4+
#include <vector>
5+
#include <iostream>
6+
#include <cstring>
7+
using namespace std;
8+
class TrieNode {
9+
public:
10+
static const int SPACE_SIZE = 26;
11+
TrieNode *children[SPACE_SIZE];
12+
bool exist;
13+
14+
TrieNode() {
15+
for (int i = 0; i < SPACE_SIZE; ++i) {
16+
children[i] = nullptr;
17+
}
18+
exist = false;
19+
}
20+
};
21+
class WordDictionary {
22+
public:
23+
WordDictionary() {
24+
root = new TrieNode();
25+
}
26+
void addWord(const string s) {
27+
TrieNode *p = root;
28+
for (char c : s) {
29+
int index = c - 'a';
30+
if (p->children[index] == nullptr) {
31+
p->children[index] = new TrieNode();
32+
}
33+
p = p->children[index];
34+
}
35+
p->exist = true;
36+
}
37+
void insert(const string s) {
38+
addWord(s);
39+
}
40+
bool search(const string key) const {
41+
return search(root, key.c_str());
42+
}
43+
private:
44+
TrieNode *root;
45+
bool search(TrieNode *p, const char *target) const {
46+
if (p == nullptr)
47+
return false;
48+
int len = strlen(target);
49+
if (target == nullptr || len == 0) {
50+
return p->exist;
51+
}
52+
char c = *target;
53+
if (c != '.') {
54+
int index = c - 'a';
55+
return search(p->children[index], target + 1);
56+
} else {
57+
for (int i = 0; i < TrieNode::SPACE_SIZE; ++i) {
58+
if (search(p->children[i], target + 1))
59+
return true;
60+
}
61+
return false;
62+
}
63+
}
64+
};
65+
int main(int argc, char **argv)
66+
{
67+
WordDictionary trie;
68+
trie.insert("bad");
69+
trie.insert("dad");
70+
trie.insert("mad");
71+
cout << trie.search("") << endl;
72+
cout << trie.search("pad") << endl;
73+
cout << trie.search("bad") << endl;;
74+
cout << trie.search(".ad") << endl;
75+
cout << trie.search("b..") << endl;
76+
cout << trie.search("mada") << endl;
77+
return 0;
78+
}

algorithms/ImplementTrie/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ class Trie {
6363
TrieNode *root;
6464
};
6565
```
66+
## 扩展
67+
68+
[Add and Search Word - Data structure design](../AddandSearchWord): 实现含有任意字符的搜索

0 commit comments

Comments
 (0)