Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

159. Accounts Merge

MediumDay 46 of 60Graphs

Each entry of accounts is a list whose first element is a person's name and the rest are email addresses. Two accounts belong to the same person if they share at least one email (people with the same name may still be different people). Merge the accounts and return them as [name, emails...] with the emails in sorted order. The accounts themselves 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"]]
The first two accounts share johnsmith@mail.com, so they merge. The last John shares nothing, so he stays separate.
Constraints
  • 1 <= accounts.length <= 1000
  • 2 <= accounts[i].length <= 10
  • 1 <= accounts[i][j].length <= 30
  • Names are English letters, emails are valid addresses
Hints
Frequently asked at: Meta, Google, Amazon, Microsoft
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for โ‚น200/ 30 min