forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
100 lines (78 loc) · 2.66 KB
/
main3.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
/// Source : https://leetcode.com/problems/implement-magic-dictionary/description/
/// Author : liuyubobobo
/// Time : 2017-11-05
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
/// Hash Map + Store-All-Patterns
///
/// Time Complexity: buildDict: O(len(dict)*w) where w is the avarage length of words in dict
/// search: O(len(search_word)^2)
/// Space Complexity: O(len(dict)*w)
class MagicDictionary {
private:
unordered_set<string> unique_words;
unordered_map<string, int> records;
public:
/** Initialize your data structure here. */
MagicDictionary() {
unique_words.clear();
records.clear();
}
/** Build a dictionary through a list of words */
void buildDict(const vector<string>& dict) {
for(string word: dict){
unique_words.insert(word);
for(int i = 0 ; i < word.size() ; i ++){
string pattern = word.substr(0, i) + "*" +
word.substr(i+1, word.size() - i - 1);
if(records.find(pattern) == records.end())
records[pattern] = 1;
else
records[pattern] += 1;
}
}
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
bool search(const string& word) {
bool inDict = (unique_words.find(word) != unique_words.end());
for(int i = 0 ; i < word.size() ; i ++){
string pattern = word.substr(0, i) + "*" +
word.substr(i+1, word.size() - i - 1);
if(inDict && records[pattern] > 1)
return true;
if(!inDict && records[pattern] > 0)
return true;
}
return false;
}
};
void printBool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
vector<string> dict1;
dict1.push_back("hello");
dict1.push_back("leetcode");
MagicDictionary obj1;
obj1.buildDict(dict1);
printBool(obj1.search("hello")); // false
printBool(obj1.search("hhllo")); // true
printBool(obj1.search("hell")); // false
printBool(obj1.search("leetcoded")); // false
cout << endl;
// ---
vector<string> dict2;
dict2.push_back("hello");
dict2.push_back("hallo");
dict2.push_back("leetcode");
MagicDictionary obj2;
obj2.buildDict(dict2);
printBool(obj2.search("hello")); // true
printBool(obj2.search("hhllo")); // true
printBool(obj2.search("hell")); // false
printBool(obj2.search("leetcoded")); // false
return 0;
}