### Problem Statement#

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.

#### 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]"]
]
``````

### DFS: Recursive#

``````from collections import defaultdict
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
"""
[name, email_1, email_2,...]
"""
mp = set()

# Create email-to-name map & adjacency set
for acc in accounts:
name = acc[0]
emails = acc[1:]

for email in emails:
mp[email] = name

# Find accounts
out = []

def DFS(u: str):
subgraph.append(u)
subgraph.append(v)

out = []
subgraph = []
DFS(u)

out = [mp[u]] + sorted(subraph)

return out
``````

### Union Find (Disjoint Set)#

See my Union Find post for a general overview of the `Union Find` (`Disjoint Set`) data structure.

``````class UnionFind:
def __init__(self, size: int) -> None:
self.root = [i for i in range(size)]
self.rank = [1] * size

def find(self, x: int) -> int:
p = x

while p != self.root[p]:
# Path compression
self.root[p] = self.root[self.root[p]]
p = self.root[p]

return p

def union(self, x: int, y: int) -> int:
a = self.find(x)
b = self.find(y)

if a == b:
return 0

# Union by rank
if self.rank[a] >= self.rank[b]:
self.root[b] = self.root[a]
self.rank[a] += self.rank[b]
else:
self.root[a] = self.root[b]
self.rank[b] += self.rank[a]

return 1

def connected(self, x: int, y: int):
return self.find(x) == self.find(y)

from collections import defaultdict
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:

uf = UnionFind(len(accounts))

email_to_account = dict()
for i, acc in enumerate(accounts):
name = acc[0]
emails = acc[1:]
for email in emails:
if email in email_to_account:
j = email_to_account[email]
uf.union(i, j)
else:
email_to_account[email] = uf.find(i)
"""
email_to_account = {
'[email protected]': 1,
'[email protected]': 0,
'[email protected]': 3,
'[email protected]': 0,
'[email protected]': 2
}
"""

account_to_emails = defaultdict(set)
for email, account in email_to_account.items():

"""
account_to_emails = defaultdict(<class 'set'>,
{1: {'[email protected]',
'[email protected]',
'[email protected]'},
2: {'[email protected]'},
3: {'[email protected]'}})
"""

out = []
for account, emails in account_to_emails.items():
name = accounts[account][0]

out.append([name] + sorted(emails))

return out
"""
[
["John","[email protected]","[email protected]","[email protected]"],
["Mary","[email protected]"],
["John","[email protected]"]
]
"""
``````

Leetcode 721: Accounts Merge