Accounts Merge - Problem

Imagine you're working at a tech company where employees can have multiple email accounts, but sometimes they create duplicate profiles in your system. Your task is to merge accounts that belong to the same person by identifying shared email addresses.

Given a list of accounts where each account contains:

  • First element: name (string)
  • Remaining elements: emails associated with that account

The Challenge: Two accounts belong to the same person if they share at least one email address. Even accounts with the same name might belong to different people, but accounts belonging to the same person will always have the same name.

Your Goal: Return merged accounts where:

  • First element is the person's name
  • Remaining elements are all their emails in sorted order
  • No duplicate emails within each account

Example:
[["John", "a@gmail.com", "b@gmail.com"], ["John", "c@gmail.com"], ["John", "a@gmail.com", "d@gmail.com"]]
โ†’ [["John", "a@gmail.com", "b@gmail.com", "c@gmail.com", "d@gmail.com"]]

The first and third accounts share a@gmail.com, so all three accounts belong to the same John!

Input & Output

Basic Account Merging
$ 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"]]
๐Ÿ’ก Note: The first and second John accounts share 'johnsmith@mail.com', so they merge. The third John has no shared emails with others, so stays separate. Mary remains unchanged.
Transitive Merging
$ Input: accounts = [["Gabe","Gabe0@m.co","Gabe3@m.co","Gabe1@m.co"],["Kevin","Kevin3@m.co","Kevin5@m.co","Kevin0@m.co"],["Ethan","Ethan5@m.co","Ethan4@m.co","Ethan0@m.co"],["Hanzo","Hanzo3@m.co","Hanzo1@m.co","Hanzo0@m.co"],["Fern","Fern5@m.co","Fern1@m.co","Fern0@m.co"]]
โ€บ Output: [["Ethan","Ethan0@m.co","Ethan4@m.co","Ethan5@m.co"],["Gabe","Gabe0@m.co","Gabe1@m.co","Gabe3@m.co"],["Hanzo","Hanzo0@m.co","Hanzo1@m.co","Hanzo3@m.co"],["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"],["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"]]
๐Ÿ’ก Note: No accounts share emails, so each person remains as a separate account with their emails sorted.
Single Account
$ Input: accounts = [["David","David0@m.co","David1@m.co"]]
โ€บ Output: [["David","David0@m.co","David1@m.co"]]
๐Ÿ’ก Note: Edge case with only one account - return it with emails in sorted order.

Visualization

Tap to expand
๐Ÿ•ต๏ธ Account Detective: The Investigation Process๐Ÿ“ Case Files (Accounts)File A: John๐Ÿ“ง john@mail.com๐Ÿ“ง john_ny@mail.comFile B: John๐Ÿ“ง john@mail.com๐Ÿ“ง john00@mail.comFile C: Mary๐Ÿ“ง mary@mail.com๐Ÿ—‚๏ธ Evidence CatalogEmail โ†’ First File Mappingjohn@mail.com โ†’ File A (index 0)john_ny@mail.com โ†’ File A (index 0)john00@mail.com โ†’ File B (index 1)mary@mail.com โ†’ File C (index 2)๐Ÿ” When john@mail.com appears in File B โ†’ Union(0,1)!๐Ÿ”— Union-Find ConnectionsA(0)root: 0B(1)root: 0C(2)root: 2Connected via john@mail.com๐Ÿ“‹ Final Grouped Files๐Ÿ‘ฅ Group 1 (Root: 0)Files: A + B (Both John accounts)Combined Evidence:["John", "john00@mail.com", "john@mail.com", "john_ny@mail.com"]๐Ÿ‘ค Group 2 (Root: 2)Files: C (Mary account)Evidence:["Mary", "mary@mail.com"]๐ŸŽฏ Case Closed: Merged Accountsโœ… Investigation CompleteJohn's files merged: 4 unique emails, Mary's file: 1 emailAll evidence catalogued and sorted alphabetically per person
Understanding the Visualization
1
Evidence Catalog
Create a master catalog where each piece of evidence (email) points to the first case file (account) that contained it
2
Connect the Dots
When you find the same evidence in multiple files, connect those files using Union-Find - they belong to the same person
3
Group Files
Find all connected case files by checking which files have the same root connection in your Union-Find structure
4
Consolidate Evidence
For each group, collect all unique evidence (emails) and organize them alphabetically
5
File Reports
Create final consolidated case files with the person's name and all their evidence sorted
Key Takeaway
๐ŸŽฏ Key Insight: This is fundamentally a graph connectivity problem! Accounts are nodes, shared emails create edges, and Union-Find efficiently finds connected components (people) in nearly linear time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— m ร— ฮฑ(n))

n accounts, m emails per account, ฮฑ is inverse Ackermann function (nearly constant)

n
2n
โœ“ Linear Growth
Space Complexity
O(n ร— m)

Email to account mapping and Union-Find parent array

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค accounts.length โ‰ค 1000
  • 2 โ‰ค accounts[i].length โ‰ค 10
  • 1 โ‰ค accounts[i][j].length โ‰ค 30
  • accounts[i][0] consists of English letters
  • accounts[i][j] (for j > 0) is a valid email address
  • All emails within the same account are unique
Asked in
Facebook 45 Google 38 Amazon 32 Microsoft 28 Apple 15
125.0K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen