Search⌘ K
AI Features

Accounts Merge

Explore how to use the union-find algorithm to merge user accounts based on common emails and names. Understand the problem constraints and implement a solution that outputs distinct user accounts with sorted, unique emails.

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 sort function that sorts the email addresses based on the ASCII value of each character.

Constraints:

  • 11 \leq accounts.length 100\leq 100

  • 22 \leq accounts[i].length 10\leq 10

  • 11 \leq accounts[i][j].length 30\leq 30

  • Because accounts[i][0] is the name of any person, it should contain only English letters.

  • For j>0j>0, accounts[i][j] should be a valid email.

Examples

Understand the problem

Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Technical Quiz
1.

Consider the following accounts:

Accounts = [ [Kate, k3@mail.com, k56@mail.com, kt@mail.com],

             [Kim, k65@mail.com, km@mail.com],

             [Kim, kkk1@mail.com, k345@mail.com, k5@mail.com],

             [Kate, k78@mail.com, k90@mail.com, kt@mail.com] ]

What is the number of accounts in the output?

A.

4

B.

3

C.

2

D.

1


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in AccountsMerge.java in the following coding playground. You will need the provided supporting code to implement your solution.

Java
usercode > AccountsMerge.java
/*
⬅️ We have provided a UnionFind.java file under the "Files" tab
of this widget. You can use this file to build your solution.
*/
import java.util.*;
class AccountsMerge {
public static List<List<String>> accountsMerge(List<List<String>> accounts){
// Replace this placeholder return statement with your code
return new ArrayList<>();
}
}
Accounts Merge