[LeetCode] Accounts Merge

721. Accounts Merge

Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.

Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.

After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.

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
class Solution {
void merge(set<set<string>>& mails, string name, vector<vector<string>>& res) {
for(auto i = begin(mails); i != end(mails); i++) {
set<string> unions = *i;
bool isMerged = false;
do {
isMerged = false;
auto j = next(i);
while (j != end(mails)) {
set<string> intersect;
set_intersection(begin(unions), end(unions), begin(*j), end(*j), inserter(intersect, intersect.begin()));
if (intersect.empty()) { j++; continue; }
isMerged = true;
unions.insert(begin(*j), end(*j));
mails.erase(j++);
}
} while(isMerged);
vector<string> mail{name};
mail.insert(mail.end(), begin(unions), end(unions));
res.push_back(mail);
}
}
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
unordered_map<string, set<set<string>>> sortedAccount;
vector<vector<string>> res;
for(auto& acc : accounts) {
sortedAccount[acc.front()].insert(set<string>(acc.begin() + 1, acc.end()));
}
for(auto& e : sortedAccount) {
merge(e.second, e.first, res);
}
return res;
}
};
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
class Solution {
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
unordered_map<string, set<int>> group;
vector<bool> merged(accounts.size(), false);
vector<vector<string>> res;
for(int i = 0; i < accounts.size(); i++) {
for(int j = 1; j < accounts[i].size(); j++) {
group[accounts[i][j]].insert(i);
}
}
for(int i = 0; i < merged.size(); i++) {
if(merged[i]) continue;
merged[i] = true;
vector<string> mail{accounts[i][0]};
unordered_set<string> s;
queue<int> q;
q.push(i);
while(!q.empty()) {
int idx = q.front();
q.pop();
for(int i = 1; i < accounts[idx].size(); i++) {
s.insert(accounts[idx][i]);
for(int ref : group[accounts[idx][i]]) {
if(merged[ref]) continue;
merged[ref] = true;
q.push(ref);
}
}
}
mail.insert(end(mail), begin(s), end(s));
sort(mail.begin() + 1, mail.end());
res.push_back(mail);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/05/15/PS/LeetCode/accounts-merge/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.