Accounts Merge

LeetCode Q 721 - Accounts Merge

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

Solution 1: Disjoint Set / Union Find

  1. build the emailToId map, where key: email, value: the root index
  2. find each email’s father email and name, then group them up

Code:

class UnionFindSet {
	int[] parent;
	public UnionFindSet (int size) {
		this.parent = new int[size];
		for (int i = 0; i < size; i++) 
			parent[i] = i;
	}

	public int find (int x) {
		if (parent[x] != x) parent[x] = find(parent[x]);
		return parent[x];
	}

	public void union (int x, int y) {
		int px = find(x), py = find(y);
		if (px != py) parent[px] = py;
	}
}

public List<List<String>> accountsMerge(List<List<String>> accounts) {
	List<List<String>> res = new ArrayList<>();
	if (accounts == null || accounts.size() == 0) return res;

	UnionFindSet ufs = new UnionFindSet(accounts.size());

	// build the emailToId map
	Map<String, Integer> emailToId = new HashMap<>();
	for (int i = 0; i < accounts.size(); i++) {
		List<String> emails = accounts.get(i);
		for (int j = 1; j < emails.size(); i++) { // skip name, iterate emails
			String email = emails.get(j); // get email
			emailToId.putIfAbsent(email, i);
			ufs.union(emailToId.get(email), i); // union the emails having the same name or in the same list. And set index i as its parent
		}
	}

	Map<Integer, List<String>> idToEmails = new HashMap<>();
	for (String email: emailToId.keySet()) {
		int idx = emailToId.get(email); // get index
		int p = ufs.find(idx); // get parent
		idToEmails.putIfAbsent(p, new ArrayList<>(); 
		idToEmails.get(p).add(email); 
	}

	for (int idx: IdToEmails.keySet()) {
		List<String> temp = new ArrayList<>(); // create account
		temp.add(accounts.get(idx).get(0)); // add name;
		List<String> emails = IdToEmails.get(idx); // get emails
		Collections.sort(emails); // sort emails
		temp.addAll(emails); // add emails
		res.add(temp); // add account
	}

	return res;
}

   Reprint policy


《Accounts Merge》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC