-
Notifications
You must be signed in to change notification settings - Fork 1
/
accounts-merge.cs
50 lines (44 loc) · 1.89 KB
/
accounts-merge.cs
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
public class Solution {
Dictionary<string,string> parents;
public IList<IList<string>> AccountsMerge(IList<IList<string>> accounts) {
var owner = new Dictionary<string, string>(); // maps all emails to a user
parents = new Dictionary<string,string>(); // maps parent mail id for each mail id
var uf = new Dictionary<string,SortedSet<string>>(); // get all emails from one primary email id
// initialize parents & owner
foreach(var acc in accounts){
for (var i = 1; i < acc.Count; i++) {
parents[acc[i]] = acc[i]; // all are self parent initially
owner[acc[i]] = acc[0]; // key is email and value is owner name
}
}
// put all emails associated to same parent email in one set
foreach(var acc in accounts){
var p = Find(acc[1]);
for (var i = 2; i < acc.Count; i++) {
var curr = Find(acc[i]);
parents[curr] = p;
}
}
// for each parent mail id, create a sorted set of linked mail ids
foreach(var acc in accounts){
var p = Find(acc[1]);
if(!uf.ContainsKey(p))
uf[p] = new SortedSet<string>(StringComparer.Ordinal);
for (var i = 1; i < acc.Count; i++)
uf[p].Add(acc[i]);
}
// create final result set
var result = new List<IList<string>>();
foreach(var key in uf.Keys){
var emails = uf[key].ToList();
emails.Insert(0, owner[key]); // Add name of person who owns all these emails
result.Add(emails);
}
return result;
}
private string Find(string s){
if(parents[s] != s)
parents[s] = Find(parents[s]);
return parents[s];
}
}