All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0701_0800.s0721_accounts_merge.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy