Accounts Merge

You are given a list of account records, accounts. In each record accounts[i], the first string is a person's name and the remaining strings are email addresses on that account.

Two records belong to the same person if they have at least one email address in common. This chains: if record A shares an email with record B, and B shares one with record C, then A, B and C all belong to one person even if A and C share nothing directly. Two records with the same name but no such link may belong to different people. All records of one person carry the same name.

Merge the records and return one entry per person, in this format: the name first, followed by every distinct email of that person sorted in plain string order (the order Array.prototype.sort() gives with no comparator). The people may be listed in any order.

Constraints

  • 1 <= accounts.length <= 1000
  • 2 <= accounts[i].length <= 10
  • 1 <= accounts[i][j].length <= 30
  • Names contain letters only. Emails contain lowercase letters, digits, . and @.

Examples

Example 1

Input: accounts = [["Ana","ana@x.com","ana.work@x.com"],["Ben","ben@y.com"],["Ana","ana.home@x.com","ana@x.com"],["Ana","a.dev@z.com"]]
Output: [["Ana","a.dev@z.com"],["Ana","ana.home@x.com","ana.work@x.com","ana@x.com"],["Ben","ben@y.com"]]

Records 0 and 2 share ana@x.com, so they are the same Ana: her emails are merged and sorted ("." sorts before "@"). Record 3 shares no email with them, so it stays a separate Ana. The outer order of the answer doesn't matter.

Example 2

Input: accounts = [["Kim","k1@m.io","k2@m.io"],["Kim","k3@m.io"],["Kim","k2@m.io","k3@m.io"]]
Output: [["Kim","k1@m.io","k2@m.io","k3@m.io"]]

Records 0 and 1 share nothing directly, but record 2 shares k2@m.io with record 0 and k3@m.io with record 1, so all three are one person.

Example 3

Input: accounts = [["Lee","lee@a.com"],["Lee","lee@b.com"]]
Output: [["Lee","lee@a.com"],["Lee","lee@b.com"]]

Same name but no shared email: two different people, two accounts.

JavaScript you'll use: Map and Set Classes for data structures Sorting and comparators

/**
 * @param {string[][]} accounts
 * @return {string[][]}
 */
function accountsMerge(accounts) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.