721. Accounts Merge

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”,“johnsmith@mail.com”,“john_newyork@mail.com”],[“John”,“johnsmith@mail.com”,“john00@mail.com”],[“Mary”,“mary@mail.com”],[“John”,“johnnybravo@mail.com”]] Output: [[“John”,“john00@mail.com”,“john_newyork@mail.com”,“johnsmith@mail.com”],[“Mary”,“mary@mail.com”],[“John”,“johnnybravo@mail.com”]] Explanation: The first and second John’s are the same person as they have the common email “johnsmith@mail.com”. 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’, ‘mary@mail.com’], [‘John’, ‘johnnybravo@mail.com’], [‘John’, ‘john00@mail.com’, ‘john_newyork@mail.com’, ‘johnsmith@mail.com’]] would still be accepted.

Whenever we must work with a set of elements (emails) that are connected (belong to the same user), we should always consider visualizing our input as a graph. In this problem, converting the input into a graph will facilitate the process of “merging” two accounts.

  • code
class Solution:
    def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
        graph = defaultdict(list)
        visited = set()
        merged_accounts = []
        
        def buildGraph(accounts):
            for account in accounts:
                first_email = account[1]
                other_emails = account[2:]
                graph[first_email].extend(other_emails)
                for other_email in other_emails:
                    graph[other_email].append(first_email)
                    
        def dfs(merged_account, email):
            visited.add(email)
            merged_account.append(email)
            for neighbour in graph[email]:
                if neighbour not in visited:
                    dfs(merged_account, neighbour)
            
        
        buildGraph(accounts)
        for account in accounts:
            name = account[0]
            first_email = account[1]
            if first_email not in visited:
                merged_account = []
                dfs(merged_account, first_email)
                merged_accounts.append([name] + sorted(merged_account))
        
        return merged_accounts