721. Accounts Merge

Question:

Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[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", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'],  ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
Explanation: 
The first and third John's are the same person as they have the common email "johnsmith@mail.com".
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', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], 
['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.

Note:

  • The length of accounts will be in the range [1, 1000].
  • The length of accounts[i] will be in the range [1, 10].
  • The length of accounts[i][j] will be in the range [1, 30].

Solution:

  • Method 1: Union find set beats 94%
     class Solution {
          public List<List<String>> accountsMerge(List<List<String>> accounts) {
              Map<String, String> owner = new HashMap<>();    // key: e-mail value: name -> records which user it belongs to.
              Map<String, String> parent = new HashMap<>();   // key: email value: email
              Map<String, TreeSet<String>> uf = new HashMap<>();
              // Step 1: Initialization
              for(List<String> account : accounts){
                  for(int i = 1; i < account.size(); i++){
                      parent.put(account.get(i), account.get(i)); // each e-mails was initialized to point to themselves.
                      owner.put(account.get(i), account.get(0));  // set onwer of each e-mail.
                  }
              }
              // Step 2: Merge each lines
              for(List<String> account : accounts){
                  String root = find(account.get(1), parent); // find the root e-mail for the first email and this one will be the root for this account.
                  for(int i = 2; i < account.size(); i++){
                      parent.put(find(account.get(i), parent), root); // find root for rest e-mails and merge them to the root.
                  }
              }
              // Step 3: Add to uf.(Actually merge different lines)
              for(List<String> account : accounts){
                  String root = find(account.get(1), parent); //// find the root e-mail for the first email.
                  if(!uf.containsKey(root)) uf.put(root, new TreeSet<String>());
                  for(int i = 1; i < account.size(); i++)
                      uf.get(root).add(account.get(i)); // Add all emails in this account to uf.
              }
              // Step 4: Create result List.
              List<List<String>> result = new ArrayList<>();
              for(String key : uf.keySet()){
                  List<String> temp = new ArrayList<>();
                  temp.add(owner.get(uf.get(key).first()));   // first add the username.
                  temp.addAll(uf.get(key)); // Add all emails to the list.
                  result.add(temp);
              }
              return result;
          }
          private String find(String s, Map<String, String> map){
              if(!map.get(s).equals(s)){
                  map.put(s, find(map.get(s), map));
              }
              return map.get(s);
          }
      }
    

Reference

  1. [Java/C++] Union Find