Comment on page

# 721.Accounts-Merge

## 题目描述

Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i] 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", "[email protected]", "[email protected]"], ["John", "[email protected]"], ["John", "[email protected]", "john_newyork@mail.com"], ["Mary", "[email protected]"]]
Output: [["John", '[email protected]', '[email protected]', '[email protected]'], ["John", "johnnybravo@mail.com"], ["Mary", "[email protected]"]]
Explanation:
The first and third John's are the same person as they have the common email "[email protected]".
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', '[email protected]'], ['John', '[email protected]'],
['John', '[email protected]', '[email protected]', '[email protected]']] 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].

## 代码

### Approach #1 DFS

{Name, 1, 2, 3} => Name -- 1 -- 2 -- 3 {Name, 2, 4, 5} => Name -- 2 -- 4 -- 5 (The same graph node 2 appears) {Name, 6, 7, 8} => Name -- 6 -- 7 -- 8
class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
Map<String, Set<String>> graph = new HashMap<>(); //<email , neighbor emails>
Map<String, String> name = new HashMap<>(); //<email, username>
// Build the graph
for (List<String> account : accounts) {
for (int i = 1; i < account.size(); i++) {
graph.putIfAbsent(account.get(i), new HashSet<>());
if (i == 1) continue;
}
}
Set<String> visited = new HashSet<>();
// DFS search the graph;
for (String email : name.keySet()) {
dfs(graph, email, visited, list);
Collections.sort(list);
}
}
return res;
}
public void dfs(Map<String, Set<String>> graph, String email, Set<String> visited, List<String> list) {
for (String next : graph.get(email)) {
dfs(graph, next, visited, list);
}
}
}
}

### Approach #2 Iterative DFS

class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
Map<String, String> emailToName = new HashMap();
Map<String, ArrayList<String>> graph = new HashMap();
for (List<String> account: accounts) {
String name = "";
for (String email: account) {
if (name == "") {
name = email;
continue;
}
emailToName.put(email, name);
}
}
Set<String> seen = new HashSet();
List<List<String>> ans = new ArrayList();
for (String email: graph.keySet()) {
if (!seen.contains(email)) {
Stack<String> stack = new Stack();
stack.push(email);
List<String> component = new ArrayList();
while (!stack.empty()) {
String node = stack.pop();
for (String nei: graph.get(node)) {
if (!seen.contains(nei)) {
stack.push(nei);
}
}
}
Collections.sort(component);
}
}
return ans;
}
}

### Approach #2: Union-Find

class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
DSU dsu = new DSU();
Map<String, String> emailToName = new HashMap();
Map<String, Integer> emailToID = new HashMap();
int id = 0;
for (List<String> account: accounts) {
String name = "";
for (String email: account) {
if (name == "") {
name = email;
continue;
}
emailToName.put(email, name);
if (!emailToID.containsKey(email)) {
emailToID.put(email, id++);
}
dsu.union(emailToID.get(account.get(1)), emailToID.get(email));
}
}
Map<Integer, List<String>> ans = new HashMap();
for (String email: emailToName.keySet()) {
int index = dsu.find(emailToID.get(email));
}
for (List<String> component: ans.values()) {
Collections.sort(component);
}
return new ArrayList(ans.values());
}
}
class DSU {
int[] parent;
public DSU() {
parent = new int;
for (int i = 0; i <= 10000; ++i)
parent[i] = i;
}
public int find(int x) {
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
public void union(int x, int y) {
parent[find(x)] = find(y);
}
}