-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_and_search_word_data_structure.cpp
51 lines (42 loc) · 1.46 KB
/
add_and_search_word_data_structure.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
//76ms, 99.75%
//the basic ieda of these problem is wrong in my solution
class WordDictionary {
public:
// Adds a word into the data structure.
void addWord(string word) {
if(_dict.find(word.size()) == _dict.end()){
vector<string> *vec = new vector<string>;
_dict.insert(pair<int, vector<string>*>(word.size(), vec));
}
_dict[word.size()]->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 search(string word) {
if(_dict.find(word.size()) == _dict.end()) return false;
vector<string> *d = _dict[word.size()];
for(int i_ = 0; i_< d->size(); ++i_){
if(match(word, (*d)[i_])) return true;
}
return false;
}
WordDictionary(){}
~WordDictionary(){
for(map<int, vector<string>*>::iterator iter = _dict.begin(); iter != _dict.end(); ++iter){
vector<string> *tmp = iter->second;
delete tmp;
}
}
private:
bool match(string a, string b){
for(int i_ = 0; i_ < a.size(); ++i_){
if(a[i_] != b[i_] && a[i_] != '.') return false;
}
return true;
}
map<int, vector<string>*> _dict;
};
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");