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.
Example:
Input: accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
Output: [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
Explanation:
The first and third John's are the same person as they have the common email "johnsmith@mail.com".
The second John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'],
['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
Approach
C++
#include <bits/stdc++.h>using namespace std;//function to find in union setint find(vector<int> &union_find, int index){while (union_find[index] != index)index = union_find[index];return index;}vector<vector<string>> accountsMerge(vector<vector<string>>&accounts){unordered_map<string, int> A;unordered_map<int, vector<string>> ret;int m = accounts.size();vector<int> union_find(m, 0);for (int i = 0; i < m; i++){union_find[i] = i;for (int j = 1; j < accounts[i].size(); j++){if (A.find(accounts[i][j]) != A.end()){int root1 = find(union_find, i);int root2 = find(union_find, A[accounts[i][j]]);union_find[root1] = root2;}elseA[accounts[i][j]] = union_find[i];}}for (auto i : A){int index = find(union_find, i.second);ret[index].push_back(i.first);}vector<vector<string>> res;for (auto it : ret){vector<string> email = it.second;sort(email.begin(), email.end());email.insert(email.begin(), accounts[it.first][0]);res.push_back(email);}return res;}int main(){vector<vector<string>> accounts = {{"John", "johnsmith@mail.com", "john_newyork@mail.com"},{"John", "johnsmith@mail.com", "john00@mail.com"},{"Mary", "mary@mail.com"},{"John", "johnnybravo@mail.com"}};vector<vector<string>> res = accountsMerge(accounts);cout << "[";for (int i = 0; i < res.size(); i++){cout << "[";for (int j = 0; j < res[i].size(); j++){cout << res[i][j];if (j != res[i].size() - 1)cout << ",";}cout << "]";if (i != res.size() - 1)cout << ",";}cout << "]";return 0;}
No comments:
Post a Comment