Merge accounts by email

Write codeHard40 minMeta style

Your task

You are given accounts, where accounts[i] = [name, email1, email2, ...]. Two accounts belong to the same person if they share at least one email; a person may have multiple accounts and any two with a common email merge transitively. Return the merged accounts: each as [name, sorted_unique_emails...], where the name is the name from any of the merged accounts (they share the same name). Return the list of merged accounts sorted ascending by their full [name, emails...] list representation.

Examples

merge_accounts(accounts: list[list[str]]) -> list[list[str]]
Example 1
Inputmerge_accounts([["John", "a@x.com", "b@x.com"], ["John", "b@x.com", "c@x.com"], ["Mary", "m@x.com"]])
Output[["John", "a@x.com", "b@x.com", "c@x.com"], ["Mary", "m@x.com"]]