Accounts Merge
Each account is a list [name, email1, email2, ...]. Two accounts belong to the same person if they share at least one common email; a name may be reused by different people. Merge all accounts of the same person and return each merged account as its name followed by its emails sorted lexicographically. Accounts may be returned in any order.
Open official problem prompt ↗Cluster all emails that belong to the same real person and present each cluster as a name plus its sorted emails.
Like consolidating duplicate contacts on a phone: if two entries share any phone number or email, they are the same person and get merged into one card.
- Input
- accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
- Output
- [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
- Why
- The first two John accounts share johnsmith@mail.com so they merge; the third John shares no email so stays separate; Mary is untouched.
1 <= accounts.length <= 10002 <= accounts[i].length <= 101 <= accounts[i][j].length <= 30accounts[i][0] consists of English lettersEmails consist of lowercase letters and exactly one '@'