Back to DSA

Accounts Merge

medium
Acceptance: 49%
GraphsUnion FindDFS

You have a list of user accounts. Each account starts with a user name followed by one or more email addresses. Two accounts belong to the same person if they share at least one email. Merge all accounts belonging to the same person. Return the merged accounts with the name first and remaining emails sorted alphabetically.

Examples

Example 1:
Input:accounts = [["Alice","a1@mail.com","a2@mail.com"],["Alice","a2@mail.com","a3@mail.com"],["Bob","b1@mail.com"]]
Output:[["Alice","a1@mail.com","a2@mail.com","a3@mail.com"],["Bob","b1@mail.com"]]
Explanation: The two Alice accounts share a2@mail.com and are merged.

Hints

00:00
1234567