-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
47 lines (44 loc) · 1005 Bytes
/
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
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class ValidWordAbbr {
unordered_map<string, string> abbs;
string getAbb(string s){
int n = s.size();
if (n <= 2) {
return s;
} else {
return s.substr(0, 1) + to_string(n - 2) + s.substr(n - 1,1);
}
}
public:
ValidWordAbbr(vector<string> &dictionary) {
for (auto w : dictionary) {
string a = getAbb(w);
if (abbs.find(a) != abbs.end()) {
abbs[a] = "";
} else {
abbs[a] = w;
}
}
}
bool isUnique(string word) {
string a = getAbb(word);
return abbs.count(a) == 0 ||
(abbs.count(a) == 1 && abbs[a] == word);
}
};
int main() {
vector<string> dict = {"deer", "door", "cake", "card"};
ValidWordAbbr sol(dict);
cout << sol.isUnique("deer") << endl;
cout << sol.isUnique("cake") << endl;
return 0;
}