forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt-and-decrypt-strings.cpp
More file actions
36 lines (32 loc) · 882 Bytes
/
encrypt-and-decrypt-strings.cpp
File metadata and controls
36 lines (32 loc) · 882 Bytes
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
// Time: ctor: O(m + d), m is len(keys), d is sum(len(x) for x in dictionary)
// encrypt: O(n)
// decrypt: O(n)
// Space: O(m + d)
// freq table
class Encrypter {
public:
Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary) {
for (int i = 0; i < size(keys); ++i) {
lookup_[keys[i]] = values[i];
}
for (const auto& x : dictionary) {
++cnt_[encrypt(x)];
}
}
string encrypt(string word1) {
string result;
for (const auto& c : word1) {
if (!lookup_.count(c)) {
return "";
}
result += lookup_[c];
}
return result;
}
int decrypt(string word2) {
return cnt_[word2];
}
private:
unordered_map<char, string> lookup_;
unordered_map<string, int> cnt_;
};