Tensorum
YouTubeশেখা শুরু করুন
LeetCode 721 · Union Find / DSU

Accounts Merge

LeetCode 721 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Union accounts sharing an email.

মাঝারিArrayHash TableStringDepth-First SearchBreadth-First Search
LeetCode-এ খুলুন ↗

আমি Accounts Merge (LeetCode 721) এভাবে সমাধান করেছি — Union Find / DSU প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Accounts that share an email belong to the same person. Map each email to the first account that had it; when another account has the same email, union the two accounts. Finally group emails by their account's root.

Approach

  1. 1

    email → first account index; union on repeats.

  2. 2

    Group each email under find(account).

  3. 3

    Output name + sorted emails per group.

আমি কীভাবে সমাধান করেছি

Disjoint set unionPythonO(M α(N) + M log M)
class DisjointSet:
    def __init__(self,n):
        self.rank = [0]*(n+1)
        self.parents = [i for i in range(n+1)]
    def find(self,u):
        if self.parents[u]==u:
            return u
        self.parents[u]=self.find(self.parents[u])
        return self.parents[u]
    def union(self,u,v):
        pu,pv = self.find(u),self.find(v)
        if pu==pv:
            return
        if self.rank[pu]<self.rank[pv]:
            self.parents[pu]=pv
        elif self.rank[pv]<self.rank[pu]:
            self.parents[pv]=pu
        elif self.rank[pv]==self.rank[pu]:
            self.parents[pv]=pu 
            self.rank[pu]+=1

class Solution:
    def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
        #account id - info 
        #email to account id mapping
        #if email is already there, connect them using disjoiint set

        #merge accounts
        #if email's final parent is same, add to the same account - parent account id, email

        #final response 
        #add name, email address
        n = len(accounts)
        if n==1:
            return accounts
        ds = DisjointSet(n)

        email_account_map = {}
        
        #M emails, N accounts
        #M*alpha(N)
        for u,account in enumerate(accounts): #M
            for email in account[1:]:
                if email in email_account_map: #alpha(N)
                     v = email_account_map[email] 
                     ds.union(u,v) 
                else:
                    email_account_map[email]=u
        
        merged_accunts = defaultdict(list)
        for email,account in email_account_map.items(): #O(M) - M unique emails
            ulp = ds.find(account) #O(alhpa(N))
            merged_accunts[ulp].append(email)
        res = []
        for account,emails in merged_accunts.items():
            name = accounts[account][0]
            res.append([name]+sorted(emails)) #MlogM
        return res

#Space Complexity - O(N) - for DSU
#Email Map - O(M)
#TC - O(M+N)

                 
স্টেপ-থ্রু লোড হচ্ছে…

Accounts Merge — সাধারণ প্রশ্ন

Accounts Merge-এর পেছনে intuition কী?

Accounts that share an email belong to the same person. Map each email to the first account that had it; when another account has the same email, union the two accounts. Finally group emails by their account's root.

Accounts Merge (LeetCode 721) কীভাবে সমাধান করবেন?

1. email → first account index; union on repeats. 2. Group each email under find(account). 3. Output name + sorted emails per group.

Accounts Merge-এর time আর space complexity কত?

Time: O(M log M)। Space: O(M + N)। (Accepted approach: Disjoint set union।)

Accounts Merge-এ সাধারণ ভুলগুলো কী?

Names can repeat across different people — only shared emails merge accounts.