-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts_merge.py
63 lines (44 loc) · 1.9 KB
/
accounts_merge.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from collections import defaultdict
class Account:
def __init__(self, name):
self.name = name
self.email = set()
def add_emails(self, emails):
for email in emails:
self.email.add(email)
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
name_accounts = defaultdict(list)
def add_account(account):
Ac = Account(account[0])
Ac.add_emails(account[1:])
name_accounts[account[0]].append(Ac)
for account in accounts:
account_name = account[0]
emails = account[1:]
not_found = True
if account_name in name_accounts:
for name_account in name_accounts[account_name]:
for email in account[1:]:
if email in name_account.email:
not_found = False
emails = account[1:]
name_account.add_emails(emails)
if not_found:
add_account(account)
else:
add_account(account)
ans = []
for name in name_accounts:
accounts = name_accounts[name]
for account in accounts:
new_ans = [account.name]
emails = []
for email in account.email:
emails.append(email)
emails.sort()
new_ans.extend(emails)
ans.append(new_ans)
print(ans)
abc = Solution()
abc.accountsMerge([["David","[email protected]","[email protected]"],["David","[email protected]","[email protected]"],["David","[email protected]","[email protected]"],["David","[email protected]","[email protected]"],["David","[email protected]","[email protected]"]])