#721

Accounts Merge

Medium Union-Find on emails

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Each account is a name followed by emails. Two accounts belong to the same person if they share any email. Merge them and return each person's name with their sorted emails.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: merge accounts that share any email.

Why this works: union all emails inside each account; emails with the same root belong to one person.

Example: John [a, b], John [b, c], John [d]

a-b-c connected → one accountd alone → another account

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Merging accounts by name — different people can share a name.

Solve on LeetCode ↗
Complexity
  • Time: O(N log N), N = total emails (sorting dominates).
  • Space: O(N).
Topic: Graphs
🎬 Episodes 74–85: video coming soon