-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2306. Naming a Company.cpp
37 lines (33 loc) · 1.23 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
/*
Problem : https://leetcode.com/problems/naming-a-company/
Author : Sabbir Musfique
Time Complexity : O(N)
Space Complexity : O(N)
*/
class Solution {
public:
long long distinctNames(vector<string>& ideas) {
// Group idea by their initials.
unordered_set<string> initialGroup[26];
for (auto& idea : ideas)
initialGroup[idea[0] - 'a'].insert(idea.substr(1));
// Calculate number of valid names from every pair of groups.
long long answer = 0;
for (int i = 0; i < 25; ++i) {
for (int j = i + 1; j < 26; ++j) {
// Get the number of mutual suffixes.
int numOfMutual = 0;
for (auto& ideaA : initialGroup[i]) {
if (initialGroup[j].count(ideaA)) {
numOfMutual++;
}
}
// Valid names are only from distinct suffixes in both groups.
// Since we can swap a with b and swap b with a to create two valid names, multiple answer by 2.
answer += 2LL * (initialGroup[i].size() - numOfMutual) * (initialGroup[j].size() - numOfMutual);
}
}
return answer;
}
//sabbir
};