Skip to content
Onsite

Accounts Merge

MediumP3Coding

Each entry of accounts is a list whose first element is a person's name and whose remaining elements are email addresses of that account. Two accounts belong to the same person if they share at least one email address. Two accounts with the same name may still belong to different people, but one person always uses a single name.

Merge the accounts. For each merged account return a list: the name first, then all of that person's distinct emails in ascending (ASCII) order. The merged accounts may be returned in any order.

Example 1

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 an email; the third John does not.

Constraints

  • 1 <= accounts.length <= 1000
  • 2 <= accounts[i].length <= 10
  • Names are 1 to 10 letters; emails are 1 to 30 characters and contain only lower-case letters, digits, '@', '.', '_'.

Hints

Run checks the 3 visible tests, Submit all 8.