-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path211.cpp
168 lines (151 loc) · 4.85 KB
/
211.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <bits/stdc++.h>
#include <gtest/gtest.h>
using namespace std;
struct Node {
char _c;
bool isEnd = false;
vector<Node *> childs;
Node() {}
Node(char c) : _c(c) {}
};
class WordDictionary {
Node *root;
bool search(string &word, int idx, Node *cur)
{
if (idx == word.size()) {
return cur->isEnd;
}
if (word[idx] == '.') {
for (Node *child : cur->childs) {
if (search(word, idx + 1, child)) {
return true;
}
}
return false;
}
else {
for (Node *child : cur->childs) {
if (child->_c == word[idx]) {
return search(word, idx + 1, child);
}
}
return false;
}
}
public:
WordDictionary() { root = new Node('#'); }
void addWord(string word)
{
Node *cur = root;
for (char &c : word) {
bool found = false;
for (Node *child : cur->childs) {
if (child->_c == c) {
cur = child;
found = true;
break;
}
}
if (!found) {
Node *newNode = new Node(c);
cur->childs.push_back(newNode);
cur = newNode;
}
}
cur->isEnd = true;
}
bool search(string word) { return search(word, 0, root); }
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/
TEST(WordDictionary, addWord)
{
WordDictionary *obj = new WordDictionary();
obj->addWord("bad");
obj->addWord("dad");
obj->addWord("mad");
EXPECT_EQ(obj->search("pad"), false);
EXPECT_EQ(obj->search("bad"), true);
EXPECT_EQ(obj->search(".ad"), true);
EXPECT_EQ(obj->search("b.."), true);
}
TEST(WordDictionary, AdditionalTests)
{
WordDictionary *obj = new WordDictionary();
// Adding words
obj->addWord("cat");
obj->addWord("car");
obj->addWord("bat");
obj->addWord("bar");
obj->addWord("rat");
obj->addWord("ran");
// Test exact matches
EXPECT_EQ(obj->search("cat"), true);
EXPECT_EQ(obj->search("car"), true);
EXPECT_EQ(obj->search("bat"), true);
EXPECT_EQ(obj->search("bar"), true);
EXPECT_EQ(obj->search("rat"), true);
EXPECT_EQ(obj->search("ran"), true);
// Test for words not added
EXPECT_EQ(obj->search("can"), false);
EXPECT_EQ(obj->search("rar"), false);
EXPECT_EQ(obj->search("tab"), false);
// Test with single wildcard
EXPECT_EQ(obj->search("c.t"), true);
EXPECT_EQ(obj->search("b.r"), true);
EXPECT_EQ(obj->search("r.t"), true);
// Test with multiple wildcards
EXPECT_EQ(obj->search("..t"), true); // matches "cat", "bat", "rat"
EXPECT_EQ(obj->search("c.."), true); // matches "cat", "car"
EXPECT_EQ(obj->search("..."), true); // matches all three-letter words
// Test edge cases (empty and very short words)
WordDictionary *obj2 = new WordDictionary();
obj2->addWord("");
EXPECT_EQ(obj2->search(""), true); // Empty word is in the dictionary
obj2->addWord("a");
EXPECT_EQ(obj2->search("a"), true); // Single character match
EXPECT_EQ(obj2->search("."), true); // Single wildcard match
EXPECT_EQ(obj2->search("b"), false); // Single character mismatch
}
// ["WordDictionary","addWord","addWord","search","search","search","search","search","search"]
// [[],["a"],["a"],["."],["a"],["aa"],["a"],[".a"],["a."]]
TEST(WordDictionary, AdditionalTests2)
{
WordDictionary *obj = new WordDictionary();
obj->addWord("a");
obj->addWord("a");
EXPECT_EQ(obj->search("."), true);
EXPECT_EQ(obj->search("a"), true);
EXPECT_EQ(obj->search("aa"), false);
EXPECT_EQ(obj->search("a"), true);
EXPECT_EQ(obj->search(".a"), false);
EXPECT_EQ(obj->search("a."), false);
}
// ["WordDictionary","addWord","addWord","addWord","addWord","search","search","addWord","search","search","search","search","search","search"]
// [[],["at"],["and"],["an"],["add"],["a"],[".at"],["bat"],[".at"],["an."],["a.d."],["b."],["a.d"],["."]]
TEST(WordDictionary, AdditionalTests3)
{
WordDictionary *obj = new WordDictionary();
obj->addWord("at");
obj->addWord("and");
obj->addWord("an");
obj->addWord("add");
EXPECT_EQ(obj->search("a"), false);
EXPECT_EQ(obj->search(".at"), false);
obj->addWord("bat");
EXPECT_EQ(obj->search(".at"), true);
EXPECT_EQ(obj->search("an."), true);
EXPECT_EQ(obj->search("a.d."), false);
EXPECT_EQ(obj->search("b."), false);
EXPECT_EQ(obj->search("a.d"), true);
EXPECT_EQ(obj->search("."), false);
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}