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 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].
问题描述
给定账户列表accounts, accounts[i]为字符串列表, accounts[i][0]为用户姓名, 其余字符串为用户邮箱
现在我们需要合并这些账户。如果两个账户存在相同的邮箱, 那么它们必定属于同一个人。注意, 即使两个账户有相同的用户姓名, 他们也可能属于不同的人, 因为不同的人可能会有相同的名字。一个人可以有任意数目的账户, 但是这些账户必定名称相同。
合并账户之后, 用以下形式返回:每个账户的第一个元素为姓名, 其余部分为排序后的邮箱。账户可以以任意顺序返回
问题分析
通过并查集或者DFS来做, 本质上是连通性问题
解法1(Union Find)
class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
ArrayList<List<String>> res = new ArrayList();
if(accounts == null || accounts.size() == 0) return res;
int len = accounts.size();
//并查集数组
int[] parents = new int[len];
for(int i = 0; i < len;i++) parents[i] = i;
//邮箱为key, 账号序号为value
Map<String, Integer> map = new HashMap();
//遍历账号序列, 使用并查集合并账号
for(int i = 0; i < len; i++){
List<String> eleStrs = accounts.get(i);
for(int j = 1; j < eleStrs.size(); j++){
String email = eleStrs.get(j);
if(!map.containsKey(email)){
map.put(email, i);
}else{
int pre_id = map.get(email);
int cur_id = i;
int pre_parent = findParent(parents, pre_id);
int cur_parent = findParent(parents, cur_id);
if(pre_parent != cur_parent){
parents[cur_parent] = pre_parent;
}
}
}
}
//key为账号序号 ,value为合并的邮箱
Map<Integer,Set<String>> mergedMap = new HashMap();
//合并邮箱
for(int i = 0;i < parents.length;i++){
int parent_id = findParent(parents, i);
if(!mergedMap.containsKey(parent_id)) mergedMap.put(parent_id, new HashSet());
for(int j = 1;j < accounts.get(i).size();j++) mergedMap.get(parent_id).add(accounts.get(i).get(j));
}
//添加用户名并且对合并邮箱排序
for(Integer id : mergedMap.keySet()){
res.add(new ArrayList());
res.get(res.size() - 1).add(accounts.get(id).get(0));
List<String> emails = new ArrayList(mergedMap.get(id));
Collections.sort(emails);
res.get(res.size() - 1).addAll(emails);
}
return res;
}
//并查集通用方法find
private int findParent(int[] parents, int id) {
while(parents[id] != id) {
parents[id] = parents[parents[id]];
id = parents[id];
}
return id;
}
}
解法2(DFS)
class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
List<List<String>> res = new ArrayList();
if(accounts == null) return res;
//图, email为节点, account[i]为边的集合
Map<String, List<String>> graph = new HashMap();
//email为key, name为value
Map<String, String> emailName = new HashMap();
//遍历accounts, 构建graph和emailName
for(List<String> account : accounts){
String name = account.get(0);
for(int i = 1;i < account.size();i++){
String email = account.get(i);
if(!graph.containsKey(email)) graph.put(email, new ArrayList<String>());
emailName.put(email, name);
if(i == 1) continue;
graph.get(email).add(account.get(i - 1));
graph.get(account.get(i - 1)).add(email);
}
}
//遍历节点, 求其对应的连通分量, 一个连通分量对应一个用户
Set<String> visited = new HashSet();
for(String email : graph.keySet()){
if(!visited.contains(email)){
List<String> mergeAccount = new LinkedList();
dfs(graph, email, mergeAccount, visited);
//注意这里, 题目要求对邮箱排序
Collections.sort(mergeAccount);
mergeAccount.add(0, emailName.get(email));
res.add(mergeAccount);
}
}
return res;
}
//利用dfs来求连通分量
public void dfs(Map<String, List<String>> graph, String email, List<String> mergeAccount, Set<String> visited){
visited.add(email);
mergeAccount.add(email);
for(String newNode : graph.get(email)){
if(!visited.contains(newNode)) dfs(graph, newNode, mergeAccount, visited);
}
}
}