🚀 Phases 1–5 are live — Days 1–17 cover the foundations and the algorithmic patterns. See the roadmap →

Accounts Merge medium

Description

Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest are emails representing emails of the account.

Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.

Merge all the accounts and return them in the following format: the first element is the name, and the rest of the list are emails in sorted order. The accounts themselves can be returned in any order.

Examples

> Case 1:
    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"]]
 
> Case 2:
    accounts = [["Gabe","Gabe0@m.co","Gabe3@m.co","Gabe1@m.co"],
                ["Kevin","Kevin3@m.co","Kevin5@m.co"],
                ["Ethan","Ethan5@m.co","Ethan4@m.co"],
                ["Hanzo","Hanzo3@m.co","Hanzo1@m.co","Hanzo0@m.co"],
                ["Fern","Fern5@m.co","Fern1@m.co","Fern0@m.co"]]
    Output: [["Ethan","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"],["Kevin","Kevin3@m.co","Kevin5@m.co"],
             ["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"]]

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.

State design / Intuition

Union accounts by shared emails:

  1. Map each email to an account index (email_to_id).
  2. For each account, union its index with all previous accounts that share any of its emails.
  3. Group account indices by their root in the union-find.
  4. For each group, collect all emails from all grouped accounts, sort them, and prepend the name.

The union-find merges accounts; the reconstruction step assembles the final output.

Code

The email_to_id map serves two purposes: (1) quickly detecting shared emails between accounts, and (2) storing all emails (as keys) for the reconstruction step. By iterating over email_to_id at the end, we naturally visit every unique email exactly once.

Analysis

  • Time: O(n × k × α(n) + E log E) where k = average emails per account and E = total emails. The sort dominates.
  • Space: O(n + E).

Same skin

  • Most Stones Removed — union by row/column index instead of email strings.
  • Redundant Connection — simpler variant, just detect the redundant edge.
  • Smallest String With Swaps — union indices by swap pairs, then sort characters within each component.
  • Number of Provinces — same connected component counting.