-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccounts-merge.py
More file actions
27 lines (19 loc) · 790 Bytes
/
accounts-merge.py
File metadata and controls
27 lines (19 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
asets = {}
for a in accounts:
name = a[0]
to_merge = set(a[1:])
new_sets = [to_merge]
for i in range(len(asets.get(name, []))):
emails = asets[name][i]
if new_sets[0].intersection(emails):
new_sets[0] = new_sets[0].union(emails)
else:
new_sets.append(emails)
asets[name] = new_sets
ret = []
for name, email_sets in asets.items():
for email_set in email_sets:
ret.append([name] + sorted(list(email_set)))
return ret