|  | 
| 1 |  | -# accounts_merge.md | 
|  | 1 | +# 721. [Accounts Merge](https://leetcode.com/problems/accounts-merge/) | 
|  | 2 | + | 
|  | 3 | +## Approach 1: Union-Find | 
|  | 4 | + | 
|  | 5 | +### Solution | 
|  | 6 | +csharp | 
|  | 7 | +```csharp | 
|  | 8 | +// Time Complexity: O(A * logA), where A is the total number of emails | 
|  | 9 | +// Space Complexity: O(A) | 
|  | 10 | +using System; | 
|  | 11 | +using System.Collections.Generic; | 
|  | 12 | + | 
|  | 13 | +public class Solution { | 
|  | 14 | +    public IList<IList<string>> AccountsMerge(IList<IList<string>> accounts) { | 
|  | 15 | +        // Map each email to a parent email | 
|  | 16 | +        Dictionary<string, string> parent = new Dictionary<string, string>(); | 
|  | 17 | +        // Map to retrieve the name associated with each email | 
|  | 18 | +        Dictionary<string, string> owner = new Dictionary<string, string>(); | 
|  | 19 | +         | 
|  | 20 | +        // Initialize parent to self and store owner for each email | 
|  | 21 | +        foreach (var account in accounts) { | 
|  | 22 | +            string ownerName = account[0]; | 
|  | 23 | +            for (int i = 1; i < account.Count; i++) { | 
|  | 24 | +                if (!parent.ContainsKey(account[i])) { | 
|  | 25 | +                    parent[account[i]] = account[i]; // Each email is its own parent initially | 
|  | 26 | +                    owner[account[i]] = ownerName; // Record owner of each email | 
|  | 27 | +                } | 
|  | 28 | +            } | 
|  | 29 | +        } | 
|  | 30 | +         | 
|  | 31 | +        // Union-Find: Union the first email with the rest in each account | 
|  | 32 | +        foreach (var account in accounts) { | 
|  | 33 | +            string firstEmail = Find(account[1], parent); | 
|  | 34 | +            for (int i = 2; i < account.Count; i++) { | 
|  | 35 | +                string nextEmail = Find(account[i], parent); | 
|  | 36 | +                parent[nextEmail] = firstEmail; | 
|  | 37 | +            } | 
|  | 38 | +        } | 
|  | 39 | +         | 
|  | 40 | +        // Group emails by their root parent | 
|  | 41 | +        Dictionary<string, SortedSet<string>> unionedEmails = new Dictionary<string, SortedSet<string>>(); | 
|  | 42 | +        foreach (var email in parent.Keys) { | 
|  | 43 | +            string rootEmail = Find(email, parent); | 
|  | 44 | +            if (!unionedEmails.ContainsKey(rootEmail)) | 
|  | 45 | +                unionedEmails[rootEmail] = new SortedSet<string>(); | 
|  | 46 | +            unionedEmails[rootEmail].Add(email); | 
|  | 47 | +        } | 
|  | 48 | +         | 
|  | 49 | +        // Construct result using owners and grouped emails | 
|  | 50 | +        List<IList<string>> result = new List<IList<string>>(); | 
|  | 51 | +        foreach (var rootEmail in unionedEmails.Keys) { | 
|  | 52 | +            List<string> emails = new List<string>(unionedEmails[rootEmail]); | 
|  | 53 | +            emails.Insert(0, owner[rootEmail]); // Add owner to the start | 
|  | 54 | +            result.Add(emails); | 
|  | 55 | +        } | 
|  | 56 | +         | 
|  | 57 | +        return result; | 
|  | 58 | +    } | 
|  | 59 | +     | 
|  | 60 | +    // Helper method to find the root of an email | 
|  | 61 | +    private string Find(string email, Dictionary<string, string> parent) { | 
|  | 62 | +        if (parent[email] != email) { | 
|  | 63 | +            parent[email] = Find(parent[email], parent); // Path compression | 
|  | 64 | +        } | 
|  | 65 | +        return parent[email]; | 
|  | 66 | +    } | 
|  | 67 | +} | 
|  | 68 | +``` | 
|  | 69 | + | 
|  | 70 | +## Approach 2: DFS to Group Emails | 
|  | 71 | + | 
|  | 72 | +### Solution | 
|  | 73 | +csharp | 
|  | 74 | +```csharp | 
|  | 75 | +// Time Complexity: O(A * N), where A is the total number of emails, and N is the average number of emails in an account | 
|  | 76 | +// Space Complexity: O(A) | 
|  | 77 | +using System; | 
|  | 78 | +using System.Collections.Generic; | 
|  | 79 | + | 
|  | 80 | +public class Solution { | 
|  | 81 | +    public IList<IList<string>> AccountsMerge(IList<IList<string>> accounts) { | 
|  | 82 | +        Dictionary<string, string> emailToName = new Dictionary<string, string>(); | 
|  | 83 | +        Dictionary<string, List<string>> graph = new Dictionary<string, List<string>>(); | 
|  | 84 | +         | 
|  | 85 | +        // Build graph and map email to owner | 
|  | 86 | +        foreach (var account in accounts) { | 
|  | 87 | +            string name = account[0]; | 
|  | 88 | +            for (int i = 1; i < account.Count; i++) { | 
|  | 89 | +                emailToName[account[i]] = name; | 
|  | 90 | +                if (!graph.ContainsKey(account[i])) | 
|  | 91 | +                    graph[account[i]] = new List<string>(); | 
|  | 92 | +                if (i == 1) continue; | 
|  | 93 | +                // Create undirected connections | 
|  | 94 | +                graph[account[i - 1]].Add(account[i]); | 
|  | 95 | +                graph[account[i]].Add(account[i - 1]); | 
|  | 96 | +            } | 
|  | 97 | +        } | 
|  | 98 | +         | 
|  | 99 | +        // Traverse graph to merge accounts | 
|  | 100 | +        HashSet<string> visited = new HashSet<string>(); | 
|  | 101 | +        List<IList<string>> result = new List<IList<string>>(); | 
|  | 102 | +         | 
|  | 103 | +        foreach (var email in emailToName.Keys) { | 
|  | 104 | +            if (!visited.Contains(email)) { | 
|  | 105 | +                List<string> list = new List<string>(); | 
|  | 106 | +                DFS(email, graph, visited, list); | 
|  | 107 | +                list.Sort(); | 
|  | 108 | +                list.Insert(0, emailToName[email]); // Add owner to the start | 
|  | 109 | +                result.Add(list); | 
|  | 110 | +            } | 
|  | 111 | +        } | 
|  | 112 | +         | 
|  | 113 | +        return result; | 
|  | 114 | +    } | 
|  | 115 | +     | 
|  | 116 | +    // Helper method for DFS traversal | 
|  | 117 | +    private void DFS(string email, Dictionary<string, List<string>> graph, HashSet<string> visited, List<string> list) { | 
|  | 118 | +        visited.Add(email); | 
|  | 119 | +        list.Add(email); | 
|  | 120 | +        foreach (var neighbor in graph[email]) { | 
|  | 121 | +            if (!visited.Contains(neighbor)) { | 
|  | 122 | +                DFS(neighbor, graph, visited, list); | 
|  | 123 | +            } | 
|  | 124 | +        } | 
|  | 125 | +    } | 
|  | 126 | +} | 
|  | 127 | +``` | 
| 2 | 128 | 
 | 
0 commit comments