Practice/Meta/Leetcode 721. Accounts Merge
CodingMust
You are given a collection of user accounts, where each account is represented as a list. The first element in each list is the account holder's name, and the remaining elements are email addresses associated with that account.
Your task is to merge accounts that belong to the same person. Two accounts definitely belong to the same person if they share at least one common email address. Note that even if two accounts have the same name, they may belong to different people if they don't share any email addresses.
After merging, each merged account should contain the person's name followed by all their email addresses in sorted order. The order of the accounts in the final result does not matter.
Example 1:
` Input: [ ["Alice", "alice@mail.com", "alice@work.com"], ["Alice", "alice@work.com", "alice@personal.com"], ["Bob", "bob@mail.com"] ]
Output: [ ["Alice", "alice@mail.com", "alice@personal.com", "alice@work.com"], ["Bob", "bob@mail.com"] ]
Explanation: The first two accounts both contain "alice@work.com", indicating they belong to the same person. All three emails are combined and sorted. Bob's account is separate. `
Example 2:
` Input: [ ["John", "john1@mail.com"], ["John", "john2@mail.com"], ["John", "john3@mail.com"] ]
Output: [ ["John", "john1@mail.com"], ["John", "john2@mail.com"], ["John", "john3@mail.com"] ]
Explanation: These are three different people who happen to have the same name, since no emails overlap. `
Example 3:
` Input: [ ["Emma", "emma@a.com", "emma@b.com"], ["Emma", "emma@b.com", "emma@c.com"], ["Emma", "emma@c.com", "emma@d.com"] ]
Output: [ ["Emma", "emma@a.com", "emma@b.com", "emma@c.com", "emma@d.com"] ]
Explanation: Account 1 and 2 share emma@b.com, account 2 and 3 share emma@c.com, so all three accounts belong to the same person through transitive connections. `