Accounts Merge
Problem
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 accounts themselves can be returned in any order.
Examples
Example 1:
Input: accounts = [["John","[email protected]","[email protected]"],["John","[email protected]","[email protected]"],["Mary","[email protected]"],["John","[email protected]"]]
Output: [["John","[email protected]","[email protected]","[email protected]"],["Mary","[email protected]"],["John","[email protected]"]]
Explanation:
The first and second John's are the same person as they have the common email "[email protected]".
The third John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [['Mary', '[email protected]'], ['John', '[email protected]'],
['John', '[email protected]', '[email protected]', '[email protected]']] would still be accepted.
Example 2:
Input: accounts =[["Gabe","[email protected]","[email protected]","[email protected]"],["Kevin","[email protected]","[email protected]","[email protected]"],["Ethan","[email protected]","[email protected]","[email protected]"],["Hanzo","[email protected]","[email protected]","[email protected]"],["Fern","[email protected]","[email protected]","[email protected]"]]
Output:[["Ethan","[email protected]","[email protected]","[email protected]"],["Gabe","[email protected]","[email protected]","[email protected]"],["Hanzo","[email protected]","[email protected]","[email protected]"],["Kevin","[email protected]","[email protected]","[email protected]"],["Fern","[email protected]","[email protected]","[email protected]"]]
Solution
Method 1 - Using DFS
We can solve this problem using graph traversal techniques:
- Graph construction: Treat emails as nodes and create edges between them whenever they appear in the same account. Use a map to associate emails with names.
- Connected components discovery: Use either DFS or Union-Find to group connected emails.
- Result generation: Collect all emails in each connected component and format the output as required (name, sorted emails).
Code
Java
class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
// Step 1: Build graph and map email to name
Map<String, String> emailToName = new HashMap<>();
Map<String, List<String>> graph = new HashMap<>();
for (List<String> acc : accounts) {
String name = acc.get(0);
for (String email : acc.subList(1, acc.size())) {
emailToName.put(email, name);
graph.computeIfAbsent(email, k -> new ArrayList<>()).add(acc.get(1));
graph.computeIfAbsent(acc.get(1), k -> new ArrayList<>()).add(email);
}
}
// Step 2: DFS to find connected components
List<List<String>> ans = new ArrayList<>();
Set<String> visited = new HashSet<>();
for (String email : graph.keySet()) {
if (!visited.contains(email)) {
List<String> group = new ArrayList<>();
dfs(email, graph, visited, group);
Collections.sort(group); // Sort emails alphabetically
group.add(0, emailToName.get(email)); // Add name at the start
ans.add(group);
}
}
return ans;
}
private void dfs(String email, Map<String, List<String>> graph, Set<String> visited, List<String> group) {
visited.add(email);
group.add(email);
for (String neigh : graph.get(email)) {
if (!visited.contains(neigh)) {
dfs(neigh, graph, visited, group);
}
}
}
}
Python
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
# Step 1: Build adjacency graph
email_to_name: Dict[str, str] = {}
graph: Dict[str, List[str]] = {}
for acc in accounts:
name = acc[0]
for email in acc[1:]:
email_to_name[email] = name
# Build edges between all emails in the same account
if email not in graph:
graph[email] = []
graph[email].append(acc[1]) # Link to the initial email
graph[acc[1]].append(email) # Link initial email to this one
# Step 2: Traverse connected components
visited = set()
ans = []
def dfs(email: str, group: List[str]):
visited.add(email)
group.append(email)
for neigh in graph[email]:
if neigh not in visited:
dfs(neigh, group)
for email in graph:
if email not in visited:
group = [] # Collect connected emails
dfs(email, group)
group.sort() # Sort emails alphabetically
ans.append([email_to_name[email]] + group)
return ans
Complexity
- ⏰ Time complexity:
O(E * log(E))- Graph construction:
O(E), whereEis the total number of emails across all accounts. - Traversal (DFS/Union-Find):
O(E)to find connected components. - Sorting emails in connected components:
O(E * log(E)). - Combined:
O(E * log(E)).
- Graph construction:
- 🧺 Space complexity:
O(E). Storage for graph (adjacency list/dict)