721. Accounts Merge
Asked at 17Meta62Amazon43Bloomberg42Google37Microsoft35Goldman Sachs28Oracle27Grab83Docusign64Airbnb57ByteDance50Anduril49LinkedIn47Rippling46Pinterest44Uber30TikTok26
Problem
Each account is [name, email1, email2, …]. Two accounts belong to the same person if they share any email (people with the same name may be different people). Merge the accounts and return each as [name, ...emails sorted]; the accounts themselves can be in any order.
Example: [["John","js@m","jny@m"],["John","js@m","j00@m"],["Mary","mary@m"]] → [["John","j00@m","jny@m","js@m"],["Mary","mary@m"]]
Union all emails within an account (to the account's first email). Then group emails by root, sort, and prefix the owner name.
Complexity: O(N log N) for N total emails (sorting).
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
parent, owner = {}, {}
def find(x):
parent.setdefault(x, x)
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for name, *emails in accounts:
for e in emails:
owner[e] = name
parent[find(e)] = find(emails[0])
groups = defaultdict(list)
for e in owner:
groups[find(e)].append(e)
return [[owner[root]] + sorted(es) for root, es in groups.items()]
acc = [["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["John", "johnsmith@mail.com", "john00@mail.com"],
["Mary", "mary@mail.com"], ["John", "johnnybravo@mail.com"]]
assert sorted(Solution().accountsMerge(acc)) == [
["John", "john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com"],
["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
assert sorted(Solution().accountsMerge([["Alex", "a@x.com"], ["Alex", "b@x.com"]])) == [["Alex", "a@x.com"], ["Alex", "b@x.com"]]
assert Solution().accountsMerge([["Bo", "b@x.com", "a@x.com", "b@x.com"]]) == [["Bo", "a@x.com", "b@x.com"]]
class Solution {
private final Map<String, String> parent = new HashMap<>();
private String find(String x) {
parent.putIfAbsent(x, x);
while (!parent.get(x).equals(x)) {
parent.put(x, parent.get(parent.get(x)));
x = parent.get(x);
}
return x;
}
public List<List<String>> accountsMerge(List<List<String>> accounts) {
Map<String, String> owner = new HashMap<>();
for (List<String> acc : accounts)
for (int i = 1; i < acc.size(); i++) {
owner.put(acc.get(i), acc.get(0));
parent.put(find(acc.get(i)), find(acc.get(1)));
}
Map<String, TreeSet<String>> groups = new HashMap<>();
for (String e : owner.keySet()) groups.computeIfAbsent(find(e), k -> new TreeSet<>()).add(e);
List<List<String>> res = new ArrayList<>();
for (Map.Entry<String, TreeSet<String>> g : groups.entrySet()) {
List<String> row = new ArrayList<>();
row.add(owner.get(g.getKey()));
row.addAll(g.getValue());
res.add(row);
}
return res;
}
}
func accountsMerge(accounts [][]string) [][]string {
parent, owner := map[string]string{}, map[string]string{}
var find func(x string) string
find = func(x string) string {
if _, ok := parent[x]; !ok {
parent[x] = x
}
for parent[x] != x {
parent[x] = parent[parent[x]]
x = parent[x]
}
return x
}
for _, acc := range accounts { // union every email of an account with its first
for _, e := range acc[1:] {
owner[e] = acc[0]
parent[find(e)] = find(acc[1])
}
}
groups := map[string][]string{}
for e := range owner {
root := find(e)
groups[root] = append(groups[root], e)
}
res := [][]string{}
for root, emails := range groups {
sort.Strings(emails)
res = append(res, append([]string{owner[root]}, emails...))
}
return res
}