g0701_0800.s0721_accounts_merge.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0701_0800.s0721_accounts_merge;
// #Medium #Array #String #Depth_First_Search #Breadth_First_Search #Union_Find
// #2022_03_24_Time_71_ms_(31.21%)_Space_68.3_MB_(18.21%)
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
@SuppressWarnings("java:S1149")
public class Solution {
public List> accountsMerge(List> accounts) {
Map emailToName = new HashMap<>();
Map> graph = new HashMap<>();
for (List account : accounts) {
String name = "";
for (String email : account) {
if (name.equals("")) {
name = email;
continue;
}
graph.computeIfAbsent(email, x -> new ArrayList<>()).add(account.get(1));
graph.computeIfAbsent(account.get(1), x -> new ArrayList<>()).add(email);
emailToName.put(email, name);
}
}
Set seen = new HashSet<>();
List> ans = new ArrayList<>();
for (String email : graph.keySet()) {
if (!seen.contains(email)) {
seen.add(email);
Stack stack = new Stack<>();
stack.push(email);
List component = new ArrayList<>();
while (!stack.empty()) {
String node = stack.pop();
component.add(node);
for (String nei : graph.get(node)) {
if (!seen.contains(nei)) {
seen.add(nei);
stack.push(nei);
}
}
}
Collections.sort(component);
component.add(0, emailToName.get(email));
ans.add(component);
}
}
return ans;
}
}