Problem

Given a listaccounts, each elementaccounts[i]is a list of strings, where the first elementaccounts[i][0]is aname, 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]", "[email protected]"], ["Mary", "[email protected]"]]

Output: [["John", '[email protected]', '[email protected]', '[email protected]'],  ["John", "[email protected]"], ["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 ofaccountswill be in the range[1, 1000].

The length ofaccounts[i]will be in the range[1, 10]

The length ofaccounts[i][j]will be in the range[1, 30].

思路一(dfs)

  • 将每一个email当成node, 然后一个account就是一个component 遍历的时候就把具有同一个node的component链接起来。 到最后,遍历每一个component,然后组成答案
    /*
        将每一个email当成node, 然后一个account就是一个component
        遍历的时候就把具有同一个node的component链接起来。
        到最后,遍历每一个component,然后组成答案

    */
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        Map<String, Node> nodes = new HashMap<>();
        Map<String, String> emailOwners = new HashMap<>();

        for (int i = 0; i < accounts.size(); i++) {
            List<String> acc = accounts.get(i);
            for (int j = 1; j < acc.size(); j++) {
                String email = acc.get(j);
                if (!nodes.containsKey(email)) {
                    nodes.put(email, new Node(email));
                    emailOwners.put(email, acc.get(0));
                }
                if (j == 1) continue;
                nodes.get(acc.get(j)).adjs.add(nodes.get(acc.get(j - 1)));
                nodes.get(acc.get(j - 1)).adjs.add(nodes.get(acc.get(j)));
            }
        }

        Set<String> visited = new HashSet<>();
        List<List<String>> res = new ArrayList<>();
        for (String email: nodes.keySet()) {
            Node curt = nodes.get(email);
            if (visited.contains(email)) continue;
            List<String> emails = new LinkedList<>();
            dfs(curt, visited, emails);
            Collections.sort(emails);
            emails.add(0, emailOwners.get(email));
            res.add(emails);
        }
        return res;
    }

    private void dfs (Node curt, Set<String> visited, List<String> emails) {
        emails.add(curt.val);
        visited.add(curt.val);

        for (Node adj: curt.adjs) {
            if (visited.contains(adj.val)) continue;
            dfs(adj, visited, emails);
        }
    }

    class Node {
        String val;
        List<Node> adjs;
        public Node (String v) {
            adjs = new ArrayList<>();
            val = v;
        }
    }

思路二(unionFind)

    1. assign itself as it's father

    2. use the email with index 0 as other emails' father's father,

    3. need to know which email corresponds to which username
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        Map<String, String> fathers = new HashMap<>();
        Map<String, String> owners = new HashMap<>();

        for (List<String> act: accounts) {
            for (int i = 1; i < act.size(); i++) {
                fathers.put(act.get(i), act.get(i));    
                owners.put(act.get(i), act.get(0));
            }
        }

        for (List<String> act: accounts) {
            String parent = find(act.get(1), fathers);
            for (int i = 2; i < act.size(); i++) {
                fathers.put(find(act.get(i), fathers), parent);
            }
        }

        Map<String, TreeSet<String>> map = new HashMap<>();
        for (String key: fathers.keySet()) {
            String parent = find(key, fathers);
            if (!map.containsKey(parent)) map.put(parent, new TreeSet<String>());
            map.get(parent).add(key);
        }

        List<List<String>> res = new ArrayList<>();
        for (String key: map.keySet()) {
            List<String> list = new ArrayList<>();
            list.add(owners.get(key));
            list.addAll(map.get(key));
            res.add(list);
        }
        return res;
    }

    private String find(String email, Map<String, String> fathers) {
        if (email == fathers.get(email)) return email;
        String p = find(fathers.get(email), fathers);
        fathers.put(email, p);
        return p;
    }

results matching ""

    No results matching ""