Accounts Merge

Problem Statement:

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, in sorted order.
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 format they were given: 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:
accounts = [[“John”, “”, “”], [“John”, “”], [“John”, “”, “”], [“Mary”, “”]]
Output: [[“John”, ‘’, ‘’, ‘’], [“John”, “”], [“Mary”, “”]]
The first and third John’s are the same person as they have the common email “”.
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’, ‘’], [‘John’, ‘’],
[‘John’, ‘’, ‘’, ‘’]] would still be accepted.

Note: This problem is part of Union-Find Series, so please be sure to go through each and every chapter in the series in order to have a solid understand of Union-Find and become really good at solving problems using Union-Find.
Please have a very good understanding of Union-Find Fundamentals before solving this problem. Through all the problems discussed in Problem Solving using Union Find one of my goals is to show you how easy it becomes writing an Union-Find based solution for even a complex problem when you are familiar with the implementation of Union-Find data structure discussed in the Template section of Union-Find Fundamentals chapter.


Let's think about the outcome of the accounts merge first. After accounts merge all the emails belong to a person will be connected to his/her name.So the person's email and all his/her emails would form a single connected component or set. This is true for all persons. So if there are N persons, after accounts merge there would be N disjoint sets or connected component.

What is an efficient way to find disjoint sets ? Union-Find data structure.

For every email we would union the email to the name of the email owner. So after the accounts merge, the name of each person would be root for all the emails of the person.

Now that we have gotten the accounts merged we just need to output them in the proper way as asked in the problem statement.

Java Code:

Login to Access Content

Python Code:

Login to Access Content

Time Complexity:

Union Find by rank without path compression: O(AlogA), where A = ∑ai and ai is the length of accounts[i].
If we use union-by-rank, this complexity improves to O(Aα(A))≈ O(A), where α is the Inverse-Ackermann function.

Space Complexity:

O(A), the space used by our Union-Find data structure.

Solution using our Union Find Template Code:

The template code is discussed in details in Union-Find Fundamentals chapter.

Java Code:

Login to Access Content

Python Code:

Login to Access Content

Please be sure to take a serious look at all the problems discussed in Union Find Problems to become from good to GREAT in solving problems using Union-Find .


If you have any feedback, please use this form:

Help Your Friends save 25% on our products