-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd_and_Search_Word_Data_structure_design.cpp
55 lines (49 loc) · 1.24 KB
/
Add_and_Search_Word_Data_structure_design.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
class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() {
}
map<int,vector<string>>m;
/** Adds a word into the data structure. */
void addWord(string word) {
int n=word.size();
m[n].push_back(word);
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool isequal(string key,string word)
{
for(int i=0;i<word.size();i++)
{
if(word[i]=='.')
{
continue;
}
else if(word[i]!=key[i])
{
return false;
}
}
return true;
}
bool search(string word) {
int n=word.size();
if(m[n].size()==0)
{
return false;
}
for(auto it:m[n])
{
if(isequal(it,word))
{
return true;
}
}
return false;
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/