Description

Given a list accounts, 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 email that is common 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

 
Example 1:
Input:
[
["John", "johnsmith@mail.com", "john00@mail.com"],
["John", "johnnybravo@mail.com"],
["John", "johnsmith@mail.com", "john_newyork@mail.com"],
["Mary", "mary@mail.com"]
] Output:
[
["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'],
["John", "johnnybravo@mail.com"],
["Mary", "mary@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. You 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']
]
is also acceptable.

 解题思路: 使用并查集。1. 建立 Email -> List[User] 关系。 2. 将属于同一Email的user 使用并查集连接。 3. 建立User -> List[Email] 4. 根据并查集 找到每个User的老大哥,以老大哥为代表输出旗下所以Email。

 public class Solution {
/**
* @param accounts: List[List[str]]
* @return: return a List[List[str]]
*/
int[] father;
public List<List<String>> accountsMerge(List<List<String>> accounts) {
father = new int[accounts.size()];
for (int i = 0; i < accounts.size(); i++) {
father[i] = i;
} //union
Map<String, List<Integer>> emailToIds = getEmailToIds(accounts);
for (List<Integer> ids: emailToIds.values()) {
for (int i = 1; i < ids.size(); i++) {
union(ids.get(i), ids.get(0));
}
} //merge
Map<Integer, Set<String>> idToEmailSet = getIdToEmailSet(accounts);
List<List<String>> mergedAccounts = new ArrayList<>();
for (Map.Entry<Integer, Set<String>> entry : idToEmailSet.entrySet()) {
Integer userId = entry.getKey();
List<String> emails = new ArrayList(entry.getValue());
Collections.sort(emails);
emails.add(0, accounts.get(userId).get(0));
mergedAccounts.add(emails);
}
return mergedAccounts;
} private Map<String, List<Integer>> getEmailToIds(List<List<String>> accounts) {
Map<String, List<Integer>> emailToIds = new HashMap<>();
for (int userId = 0; userId < accounts.size(); userId++) {
List<String> account = accounts.get(userId);
for (int i = 1; i < account.size(); i++) {
List<Integer> ids = emailToIds.getOrDefault(account.get(i), new ArrayList<Integer>());
ids.add(userId);
emailToIds.put(account.get(i), ids);
}
}
return emailToIds;
} private Map<Integer, Set<String>> getIdToEmailSet(List<List<String>> accounts) {
Map<Integer, Set<String>> idToEmailSet = new HashMap<>();
for (int userId = 0; userId < accounts.size(); userId++) {
List<String> account = accounts.get(userId);
int root = find(userId);
Set<String> emails = idToEmailSet.getOrDefault(root, new HashSet<String>());
for (int i = 1; i < account.size(); i++) {
emails.add(account.get(i));
}
idToEmailSet.put(root, emails);
}
return idToEmailSet;
} private void union (int id1, int id2) {
int root1 = find(id1);
int root2 = find(id2);
if (root1 != root2) {
father[root1] = root2;
}
} private int find(int id) {
int node = id;
while (father[node] != node) {
node = father[node];
}
int root = node;
while (id != root) {
int tmp = father[id];
father[id] = root;
id = tmp;
}
return root;
} }
05-29 00:30