Accounts Merge
Master this topic with zero to advance depth.
Accounts Merge
Given a list of accounts where each element accounts[i] is a list of strings, the first element accounts[i][0] is a name, and the rest are emails. Merge accounts belonging to the same person. Two accounts belong to the same person if there is some common email to both accounts.
Examples
Level I: DFS (Graph of Emails)
Intuition
Build an adjacency list where each email is a node and edges connect emails within the same account. Use DFS to find connected components of emails.
Detailed Dry Run
Account 1: {a, b}. Account 2: {c, a}. Graph: a-b, c-a. Component: {a, b, c}. Sort and add name 'John'.
Level III: Union-Find (Email Mapping)
Intuition
Use DSU to union all emails within an account. After processing all accounts, group emails by their root parent, sort them, and format the output.
Detailed Dry Run
Initial: {a:a, b:b, c:c}. Account 1: Union(a, b) -> {a:b, b:b}. Account 2: Union(c, a) -> {c:b, a:b}. All point to 'b'.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.