Data Structures19 sections · 729 units
Open in Course

Accounts Merge Solution

Implement your solution

Implementation details:

1.1. Email ownership: Map each email to the first account that owns it

2.2. Union on collision: If an email is already owned, union the accounts

3.3. Collect by root: After all unions, group emails by their root account

4.4. Sort and format: Each result entry is [name, sorted emails] Edge cases:

  • Same name, different people (accounts don't merge unless they share an email)
  • Single account (no merging needed)
  • All accounts merge into one

Time: O(nkα(n))O(nk \cdot \alpha(n)) for unions where kk is average emails per account, plus O(nklog(nk))O(nk \log(nk)) for sorting. Space: O(nk)O(nk) for email map and result.