forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
94 lines (72 loc) · 2.34 KB
/
main.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
/// Source : https://leetcode.com/problems/implement-magic-dictionary/description/
/// Author : liuyubobobo
/// Time : 2017-11-05
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
/// Hash Map + Bucket-By-Length
/// Time Complexity: buildDict: O(len(dict)*w) where w is the avarage length of words in dict
/// search: O(len(dict)*len(search_word))
/// Space Complexity: O(len(dict)*w)
class MagicDictionary {
private:
unordered_map<int, vector<string>> records;
public:
/** Initialize your data structure here. */
MagicDictionary() {
records.clear();
}
/** Build a dictionary through a list of words */
void buildDict(const vector<string>& dict) {
for(string word: dict){
if(records.find(word.size()) == records.end())
records[word.size()] = vector<string>();
records[word.size()].push_back(word);
}
}
/** 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) {
if(records.find(word.size()) == records.end())
return false;
for(string aWord: records[word.size()])
if(dis(aWord, word) == 1)
return true;
return false;
}
private:
int dis(const string& a, const string& b){
int res = 0;
for(int i = 0 ; i < a.size() ; i ++)
if(a[i] != b[i])
res ++;
return res;
}
};
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;
}