721. Accounts Merge
Practice Accounts Merge for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Each entry in accounts is a list whose first element is a name and whose other elements are email addresses. Two accounts belong to the same person if they share at least one email; this links transitively. Accounts with the same name but no shared email may be different people. Every person has one name.
Merge the accounts. Return one list per person: the name first, then all of that person's distinct emails sorted in ascending (ASCII) order. The people can be listed in any order.
Example
accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],
["John","johnsmith@mail.com","john00@mail.com"],
["Mary","mary@mail.com"],
["John","johnnybravo@mail.com"]]
-> [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],
["Mary","mary@mail.com"],
["John","johnnybravo@mail.com"]]
Constraints
- 1 <= accounts.length <= 1000
- 2 <= accounts[i].length <= 10
- Names and emails are made of letters, digits,
_,.and@