-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path288.cpp
34 lines (34 loc) · 1.2 KB
/
288.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
__________________________________________________________________________________________________
class ValidWordAbbr {
public:
ValidWordAbbr(vector<string> &dictionary) {
for (auto a : dictionary) {
string k = a[0] + to_string(a.size() - 2) + a.back();
m[k].insert(a);
}
}
bool isUnique(string word) {
string k = word[0] + to_string(word.size() - 2) + word.back();
return m[k].count(word) == m[k].size();
}
private:
unordered_map<string, set<string>> m;
};
__________________________________________________________________________________________________
class ValidWordAbbr {
public:
ValidWordAbbr(vector<string> &dictionary) {
for (auto a : dictionary) {
string k = a[0] + to_string(a.size() - 2) + a.back();
if (m.find(k) != m.end() && m[k] != a) m[k] = "";
else m[k] = a;
}
}
bool isUnique(string word) {
string k = word[0] + to_string(word.size() - 2) + word.back();
return m.find(k) == m.end() || m[k] == word;
}
private:
unordered_map<string, string> m;
};
__________________________________________________________________________________________________