forked from neerazz/FAANG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountsMerge.java
131 lines (125 loc) · 6.39 KB
/
AccountsMerge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.*;
/**
* Created on: Jul 23, 2020
* Questions: https://leetcode.com/problems/accounts-merge/
*/
public class AccountsMerge {
public static void main(String[] args) {
System.out.println("********************************* Solution 1 ***********************************");
System.out.println("Actual : " +
accountsMerge(Arrays.asList(
Arrays.asList("John", "[email protected]", "[email protected]"),
Arrays.asList("John", "[email protected]"),
Arrays.asList("John", "[email protected]", "[email protected]"),
Arrays.asList("Mary", "[email protected]")))
+ "\nExpected : [[John, [email protected], [email protected], [email protected]], [John, [email protected]], [Mary, [email protected]]]");
System.out.println("Actual : " +
accountsMerge(Arrays.asList(
Arrays.asList("David", "[email protected]", "[email protected]"),
Arrays.asList("David", "[email protected]", "[email protected]"),
Arrays.asList("David", "[email protected]", "[email protected]"),
Arrays.asList("David", "[email protected]", "[email protected]"),
Arrays.asList("David", "[email protected]", "[email protected]")))
+ "\nExpected : [[David, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]]");
System.out.println("********************************* Solution 2 ***********************************");
System.out.println("Actual : " +
accountsMerge_rev2(Arrays.asList(
Arrays.asList("John", "[email protected]", "[email protected]"),
Arrays.asList("John", "[email protected]"),
Arrays.asList("John", "[email protected]", "[email protected]"),
Arrays.asList("Mary", "[email protected]")))
+ "\nExpected : [[John, [email protected], [email protected], [email protected]], [John, [email protected]], [Mary, [email protected]]]");
System.out.println("Actual : " +
accountsMerge_rev2(Arrays.asList(
Arrays.asList("David", "[email protected]", "[email protected]"),
Arrays.asList("David", "[email protected]", "[email protected]"),
Arrays.asList("David", "[email protected]", "[email protected]"),
Arrays.asList("David", "[email protected]", "[email protected]"),
Arrays.asList("David", "[email protected]", "[email protected]")))
+ "\nExpected : [[David, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]]");
}
public static List<List<String>> accountsMerge_rev2(List<List<String>> accounts) {
Map<String, Integer> emailMap = new HashMap<>();
Map<Integer, Set<Integer>> graph = new HashMap<>();
Map<Integer, String> nameMap = new HashMap<>();
int size = accounts.size();
for (int i = 0; i < size; i++) {
List<String> emails = accounts.get(i);
nameMap.put(i, emails.get(0));
for (int j = 1; j < emails.size(); j++) {
String email = emails.get(j);
if (emailMap.containsKey(email)) {
int pre = emailMap.get(email);
graph.computeIfAbsent(i, val -> new HashSet<>()).add(pre);
graph.computeIfAbsent(pre, val -> new HashSet<>()).add(i);
}
emailMap.put(email, i);
}
}
// Perform a DFS and get all the linked emails.
List<List<String>> op = new ArrayList<>();
boolean[] visited = new boolean[size];
for (int i = 0; i < size; i++) {
if (!visited[i]) {
LinkedList<String> values = new LinkedList<>(DFS(accounts, visited, i, graph));
Collections.sort(values);
values.addFirst(nameMap.get(i));
op.add(values);
}
}
return op;
}
private static Set<String> DFS(List<List<String>> accounts, boolean[] visited, int start, Map<Integer, Set<Integer>> graph) {
Set<String> cur = new HashSet<>(accounts.get(start).subList(1, accounts.get(start).size()));
visited[start] = true;
for (int dep : graph.getOrDefault(start, new HashSet<>())) {
if (!visited[dep]) {
cur.addAll(DFS(accounts, visited, dep, graph));
}
}
return cur;
}
public static List<List<String>> accountsMerge(List<List<String>> accounts) {
Map<Integer, String> nameMap = new HashMap<>();
Map<String, Integer> emailMap = new HashMap<>();
Map<Integer, Set<Integer>> graph = new HashMap<>();
int len = accounts.size();
for (int i = 0; i < len; i++) {
List<String> account = accounts.get(i);
nameMap.put(i, account.get(0));
for (int j = 1; j < account.size(); j++) {
String email = account.get(j);
if (emailMap.containsKey(email)) {
int to = emailMap.get(email);
graph.computeIfAbsent(i, val -> new HashSet<>()).add(to);
graph.computeIfAbsent(to, val -> new HashSet<>()).add(i);
}
emailMap.put(email, i);
}
}
boolean[] visited = new boolean[len];
List<List<String>> op = new ArrayList<>();
// Perform a dfs for each account.
for (int i = 0; i < len; i++) {
if (!visited[i]) {
Set<String> emails = new HashSet<>();
dfs(i, accounts, graph, visited, emails);
LinkedList<String> cur = new LinkedList<>(emails);
Collections.sort(cur);
cur.addFirst(nameMap.get(i));
op.add(cur);
}
}
return op;
}
private static void dfs(int start, List<List<String>> accounts, Map<Integer, Set<Integer>> graph, boolean[] visited, Set<String> emails) {
// Add all the emails to the set and make recursive calls.
emails.addAll(accounts.get(start).subList(1, accounts.get(start).size()));
visited[start] = true;
for (int dep : graph.getOrDefault(start, new HashSet<>())) {
if (!visited[dep]) {
dfs(dep, accounts, graph, visited, emails);
}
}
}
}