accounts merge wip

🏠

Given a list accounts, 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 email that is common 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.

 1Example 1:
 2Input: 
 3accounts = [
 4 ["John", "johnsmith@mail.com", "john00@mail.com"],
 5 ["John", "johnnybravo@mail.com"],
 6 ["John", "johnsmith@mail.com", "john_newyork@mail.com"],
 7 ["Mary", "mary@mail.com"]
 8 ]
 9 
10Output: [
11 ["John",
12     'john00@mail.com',
13     'john_newyork@mail.com',
14     'johnsmith@mail.com'
15 ],
16 ["John", "johnnybravo@mail.com"],
17 ["Mary", "mary@mail.com"]]
18
19Explanation: 
20
21The first and third John's are the same person as they have the common email "johnsmith@mail.com".
22
23The second John and Mary are different people as none of their email addresses are used by other accounts.
24
25We could return these lists in any order, for example the answer [
26['Mary', 'mary@mail.com'],
27['John', 'johnnybravo@mail.com'], 
28['John', 
29 'john00@mail.com',
30 'john_newyork@mail.com',
31 'johnsmith@mail.com']
32] would still be accepted.
33
34Note:
35
36The length of accounts will be in the range [1, 1000].
37The length of accounts[i] will be in the range [1, 10].
38The length of accounts[i][j] will be in the range [1, 30].
 1import collections
 2
 3def accountsMerge( accounts):
 4    em_to_name = {}
 5    graph = collections.defaultdict(set)
 6    for acc in accounts:
 7        em_to_name[acc[1]] = acc[0]
 8        for email in acc[1:]:
 9            graph[acc[1]].add(email)
10            graph[email].add(acc[1])
11
12    def visit(email):
13        seen.add(email)
14        for next_node in graph[email]:
15            if next_node not in seen:
16                visit(next_node)
17
18    seen, done, ans = set(), set(), []
19    for email in em_to_name:
20        if email not in done:
21            visit(email)
22            ans.append([em_to_name[email]] + [*sorted([*seen])])
23            done.update(seen)
24            seen.clear()
25    return ans
26
27r = accountsMerge([
28                ["David", "d0", "d1", "d3"],
29                ["David", "d1", "d2"],
30                ["Mary", "m1", "m2"],
31                ["Mary", "m2"]])
32print(r)

1r = accountsMerge([
2                ["David", "d0", "d1", "d3"],
3                ["David", "d1", "d2"],
4                ["David", "d4", "d2"],
5                ["David", "d5", "d2"],
6                ["David", "d0", "d2"],
7                ["David", "d0", "d2", "d7"],
8                ["Mary", "m1", "m2"],
9                ["Mary", "m2"]])
graph TD d0 --> d1 d7 --> d0 d0 --> d2 d0 --> d0 d5 --> d2 d2 --> d4 d1 --> d0 d4 --> d4 d2 --> d1 d0 --> d7 d2 --> d5 d0 --> d3 d1 --> d1 d2 --> d0 d3 --> d0 d1 --> d2 d4 --> d2 d5 --> d5
graph TD m2 --> m1 m1 --> m1 m2 --> m2 m1 --> m2

See also:

alien dictionary