File tree Expand file tree Collapse file tree 3 files changed +144
-0
lines changed Expand file tree Collapse file tree 3 files changed +144
-0
lines changed Original file line number Diff line number Diff line change
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树实现
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -63,3 +63,6 @@ class Trie {
63
63
TrieNode * root;
64
64
};
65
65
```
66
+ ## 扩展
67
+
68
+ [Add and Search Word - Data structure design](../AddandSearchWord): 实现含有任意字符的搜索
You can’t perform that action at this time.
0 commit comments