Solution: Accounts Merge
Let's solve the Accounts Merge problem using the Union Find pattern.
We'll cover the following
Statement#
You are given a 2D array, accounts, where each row, accounts[i], is an array of strings, such that the first element, accounts[i][0], is a name, while the remaining elements are emails associated with that account. Your task is to determine if two accounts belong to the same person by checking if both accounts have the same name and at least one common email address.
If two accounts have the same name, they might belong to different people since people can have the same name. However, all accounts that belong to one person will have the same name. This implies that a single person can hold multiple accounts.
The output should be a 2D array in which the first element of each row is the name, and the rest of the elements are the merged list of that user’s email addresses in sorted order. There should be one row for each distinct user, and for each user, each email address should be listed only once.
Note: Please use a
sortfunction that sorts the email addresses based on the ASCII value of each character.
Constraints:
accounts.lengthaccounts[i].lengthaccounts[i][j].lengthBecause
accounts[i][0]is the name of any person, it should contain only English letters.For
, accounts[i][j]should be a valid email.
Solution#
Since the problem involves merging the accounts that have common emails among them, we'll use the union find pattern to solve it. The union find pattern is used to group elements into sets based on a specified property. Each set forms a tree data structure and has a representative element that resides at the root of the tree. The pattern uses a disjoint set data structure, such as an array, to keep track of the membership of each element in the set.
To use the union find pattern, we create and use the UnionFind class, which has two primary methods:
find(node): Returns the set’s main representative, the root parent, of a given node.union(node1, node2): Merges the sets ofnode1andnode2into one.
First, we assign a unique ID (an integer value) to each account, making them the representative of their sets. For example, if we have the accounts = parents =
Next, we iterate over the given accounts, and for each account, we iterate over its associated emails and map them to one of the parents. If an email is seen for the first time, it gets the ID of its current parent. However, if the email is being seen for the second time, that is, if two accounts share the same email, we take the union of this email’s previously assigned ID and its current ID to make a connection between them. For example, for accounts = union(0,2), where parents =
After the mapping above, we merge all the accounts sharing emails into single accounts. To achieve this, we use find(node) for each email to check who is the root parent of the given node. Finally, we go over each account and sort the emails within that account. According to our example, the final merged accounts will look like this:
Let’s look at the following illustration to get a better understanding of the solution:
1 of 15
2 of 15
3 of 15
4 of 15
5 of 15
6 of 15
7 of 15
8 of 15
9 of 15
10 of 15
11 of 15
12 of 15
13 of 15
14 of 15
15 of 15
Let’s have a look at the code for the algorithm we just discussed.
Time complexity#
Finding the root parents of all the emails to merge them will take
Space complexity#
The space complexity for this solution is
Accounts Merge
Final Remarks