Imagine you're working at a tech company where employees can have multiple email accounts, but sometimes they create duplicate profiles in your system. Your task is to merge accounts that belong to the same person by identifying shared email addresses.
Given a list of accounts where each account contains:
- First element:
name(string) - Remaining elements:
emailsassociated with that account
The Challenge: Two accounts belong to the same person if they share at least one email address. Even accounts with the same name might belong to different people, but accounts belonging to the same person will always have the same name.
Your Goal: Return merged accounts where:
- First element is the person's name
- Remaining elements are all their emails in sorted order
- No duplicate emails within each account
Example:[["John", "a@gmail.com", "b@gmail.com"], ["John", "c@gmail.com"], ["John", "a@gmail.com", "d@gmail.com"]]
โ [["John", "a@gmail.com", "b@gmail.com", "c@gmail.com", "d@gmail.com"]]
The first and third accounts share a@gmail.com, so all three accounts belong to the same John!
Input & Output
Visualization
Time & Space Complexity
n accounts, m emails per account, ฮฑ is inverse Ackermann function (nearly constant)
Email to account mapping and Union-Find parent array
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 address
- All emails within the same account are unique