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 1:
Input:
accounts = [["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.
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.
Note:
The length of accounts will be in the range [1, 1000].
The length of accounts[i] will be in the range [1, 10].
The length of accounts[i][j] will be in the range [1, 30].
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/accounts-merge
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
用并集,把所有属于同一个人的email都关联起来,再和email的owner组合。
import java.util.Map.Entry;
class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
Map<String, String> root = new HashMap<>();
Map<String, String> owner = new HashMap<>();
Map<String, Set<String>> emailUnion = new HashMap<>();
// root放的是当前email和它同属一个owner的另外一个email的对应关系。先初始化为自身。
// 如果两个点属于相同的组,就将其中一个点的 root 值赋值为另一个点的位置,这样只要是相同组里的两点,通过 find 函数得到相同的值。
// owner是每个emial对应的owner。
for (List<String> list : accounts) {
for (int i = 1; i < list.size(); i++) {
root.put(list.get(i), list.get(i));
owner.put(list.get(i), list.get(0));
}
}
// 第一个遍历,从给定的accounts中取出一个list,然后取出一个email,让这个list中的其他所有的email,都在email的map中跟这个对应起来。
// 把所有的email属于同一个owner的,都关联起来。
for (List<String> list : accounts) {
String pattern = find(list.get(1), root);
for (int i = 1; i < list.size(); i++) {
root.put(find(list.get(i), root), pattern);
}
}
// emailUnion,key值是其中一个email,value值是跟他相同owner的所有email的set集合
for (List<String> list : accounts) {
for (int i = 1; i < list.size(); i++) {
String pattern = find(list.get(i), root);
if (!emailUnion.containsKey(pattern)) {
emailUnion.put(pattern, new HashSet<>());
}
emailUnion.get(pattern).add(list.get(i));
}
}
// 把emailUnion整理出来,在owner中找到名字。组成最后答案。
List<List<String>>ans = new ArrayList<>();
for (Entry<String, Set<String>>entry: emailUnion.entrySet()) {
List<String> list = new ArrayList<>();
list.add(owner.get(entry.getKey()));
for (String value :entry.getValue()) {
list.add(value);
}
Collections.sort(list);
ans.add(list);
}
return ans;
}
String find(String email, Map<String, String> map) {
if (map.get(email).compareTo(email) == 0) {
return map.get(email);
} else {
return find(map.get(email), map);
}
}
}