721. Accounts Merge (Emails)

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

Thoughts:

  1. Use Union-Find: since the goal is to grouping emails whenever there is a connected path (common emails are analogous to shared nodes in the graph)
  2. similar idea can applies to DFS: to group accounts by traversing the connected graph

Code: T: O(V + E); S: O(V + E)

class Solution {
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        // initialize the union find & owner info
        Map<String, String> parent = new HashMap<>(); // Map<String, Integer> rank = new HashMap<>();
        Map<String, TreeSet<String>> union = new HashMap<>();

        for(List<String> a: accounts){
            for (int i = 1; i < a.size(); ++i){
                parent.put(a.get(i), a.get(i));
               // rank.put(a.get(i), 0);
            }
        }

        for(List<String> a: accounts){
            String p = find(a.get(1), parent);
            // merge the emails accounts
            for(int i = 2; i < a.size(); ++i){
                String pi = find(a.get(i), parent);
                // union
                // by rank
                // if(rank.get(pi) > rank.get(p)){
                //     String tmp = pi;
                //     pi = p;
                //     p = tmp;
                // }
                // int inc = rank.get(pi) == rank.get(p)?1:0;
                // rank.put(p, rank.get(p) + inc);
                parent.put(pi, p);
            }
        }

        // build the union where union<represented email, set of email accounts>
        for(List<String> a: accounts){
            String p = find(a.get(1), parent);
            if(!union.containsKey(p)) {
                union.put(p, new TreeSet<>());
                union.get(p).add(a.get(0));// add name first
            }
            for(int i = 1; i < a.size(); ++i){
                union.get(p).add(a.get(i)); 
            }
        }

        List<List<String>> res = new ArrayList<>();
        for(String p : union.keySet()){
            List<String> email = new ArrayList<>(union.get(p));
            res.add(email);
        }

        return res;
    }

    private String find(String q, Map<String, String> parent){
        while(parent.get(q) != q){
            // path compression
            // parent.put(q, parent.get(parent.get(q)));
            q = parent.get(q);
        }

        return q;
    }
}

results matching ""

    No results matching ""