Description

Given a list of accounts where each element accounts[i] is a list of strings, the first element accounts[i][0] is a name, and the rest are emails. Merge accounts that share common emails. Return accounts in sorted order.

Examples

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"],["John","johnnybravo@mail.com"],["Mary","mary@mail.com"]]
Explanation:

First two Johns share 'johnsmith@mail.com' so they merge.

Input:["David","david.work@company.com","d.personal@gmail.com"],["Sarah","sarah123@yahoo.com"],["David","d.personal@gmail.com","david.old@hotmail.com"],["Sarah","sarah123@yahoo.com","s.johnson@outlook.com"]]
Output:[["David","d.personal@gmail.com","david.old@hotmail.com","david.work@company.com"],["Sarah","s.johnson@outlook.com","sarah123@yahoo.com"]]
Explanation:

The two David accounts share 'd.personal@gmail.com' so they merge into one account with all three emails sorted. The two Sarah accounts share 'sarah123@yahoo.com' so they also merge. Final result has accounts sorted by name.

Input:[["Alice","alice@test.com"],["Bob","bob1@mail.com","bob2@mail.com"],["Charlie","charlie@web.com","c.smith@domain.com"],["Alice","different@email.com"]]
Output:[["Alice","alice@test.com"],["Alice","different@email.com"],["Bob","bob1@mail.com","bob2@mail.com"],["Charlie","c.smith@domain.com","charlie@web.com"]]
Explanation:

Even though there are two Alice accounts, they share no common emails so they remain separate. This demonstrates that accounts merge only when they share at least one email address, not just the same name.

Constraints

  • 1 ≤ accounts.length ≤ 1000
  • 2 ≤ accounts[i].length ≤ 10

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!