AlgoInsight

DSA problem

Accounts Merge

MediumUnion FindPattern: Union Findtop300FacebookAmazonGoogleMicrosoftBloomberg

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 of the elements are emails representing emails of the account. Now, we would like to merge these accounts. 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. After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order.

The natural abstraction is: emails are nodes, and any two emails appearing in the same account must end up in the same component. Disjoint Set Union (DSU) is the cleanest data structure here — we assign every unique email a stable integer id via a hash map, then for each account union the first email with every other email in that account.

After processing all accounts, we walk the email list again, find the root of each email, and group emails by root id into a result map. Within each group we sort the emails lexicographically (LeetCode requires sorted output), then prepend the owner's name (recovered from any email's original account).

This avoids the awkward DFS approach where you would have to build a multi-graph between every pair of emails in an account. With path compression and union by rank, the total complexity is O(N * alpha(N) + N log N) where N is the total number of emails — the log factor comes purely from the final per-group sort.

Examples

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"]]
Explanation: The first two John accounts share johnsmith@mail.com and are merged. The third John has no overlap, so it stays separate. Mary is unrelated.
Example 2
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"],["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"],["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"]]
Explanation: No two accounts share an email, so each one is returned with emails sorted lexicographically.
Example 3
Input: accounts = [["David","David0@m.co","David1@m.co"],["David","David3@m.co","David4@m.co"],["David","David4@m.co","David5@m.co"],["David","David2@m.co","David3@m.co"],["David","David1@m.co","David2@m.co"]]
Output: [["David","David0@m.co","David1@m.co","David2@m.co","David3@m.co","David4@m.co","David5@m.co"]]
Explanation: Through chained overlaps (0-1, 1-2, 2-3, 3-4, 4-5), all five David accounts merge into a single account containing six sorted emails.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

union-findhash-tablestringgraph

Browse all DSA problems