-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2306.cpp
More file actions
50 lines (49 loc) · 1.56 KB
/
2306.cpp
File metadata and controls
50 lines (49 loc) · 1.56 KB
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
class Solution {
public:
long long distinctNames(vector<string>& ideas) {
unordered_map<char, unordered_set<string>> hashMap;
for (auto& idea : ideas) {
hashMap[idea[0]].insert(idea.substr(1));
}
long long res = 0;
for (int i = 0; i < 26; ++i) {
for (int j = i + 1; j < 26; ++j) {
char a = i + 'a';
char b = j + 'a';
auto& setA = hashMap[a];
auto& setB = hashMap[b];
int intersect = 0;
for (auto& s : setA) {
if (setB.find(s) != setB.end()) intersect++;
}
int setADiff = setA.size() - intersect;
int setBDiff = setB.size() - intersect;
res += (long long)setADiff * setBDiff * 2;
}
}
return res;
}
};
// v2
class Solution {
public:
long long distinctNames(vector<string>& ideas) {
unordered_map<char, unordered_set<string>> mp;
for (auto& idea : ideas) {
mp[idea[0]].insert(idea.substr(1));
}
long long res = 0;
for (char i = 'a'; i <= 'z'; ++i) {
for (char j = i + 1; j <= 'z'; ++j) {
int m = mp[i].size();
int n = mp[j].size();
int intersect = 0;
for (auto& suffix : mp[j]) {
if (mp[i].count(suffix)) intersect++;
}
res += (long long)(m - intersect) * (long long)(n - intersect) * 2;
}
}
return res;
}
};