-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
2306-naming-a-company.cpp
46 lines (33 loc) · 1.13 KB
/
2306-naming-a-company.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
//time O(n)
//space O(n)
class Solution {
public:
long long distinctNames(vector<string>& ideas) {
unordered_map<char,unordered_set<string>> dict;
for(const string& idea : ideas){
dict[idea[0]].insert(idea.substr(1));
}
if(dict.size() < 2){
return 0;
}
long long count = 0;
for(char a = 'a'; a <= 'z' ; a++){
if(dict.find(a) == dict.end())
continue;
for(char b = a+1; b <= 'z'; b++){
if(dict.find(b) == dict.end())
continue;
int aKeys = dict[a].size();
int bKeys = dict[b].size();
for(const string& suffix : dict[a]){
if(dict[b].find(suffix) != dict[b].end()){
aKeys--;
bKeys--;
}
}
count += 2 * (aKeys*bKeys);
}
}
return count;
}
};