-
Notifications
You must be signed in to change notification settings - Fork 0
/
Account Merge
176 lines (143 loc) · 6.42 KB
/
Account Merge
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
string find(string s,map<int,int>&p)
{
return p[s]==s?s:find(p[s],p);
}
vector<vector<string>> accountsMerge(vector<vector<string>> &accounts) {
map<string,string>owner;
map<string,string>parent;
map<string,set<string>>unions;
for(int i=0;i<accounts.size();i++)
{
for(int j=1;j<accounts[i].size();j++)
{
parents[accounts[i][j]]=accounts[i][j];
owner[accounts[i][j]]=accounts[i][0];
}
}
for(int i=0;i<accounts.size();i++)
{
string p=find(accounts[i][1],parents);
for(int j=2;j<accounts[i].size();j++)
{
parents[find(accounts[i][j],parents)]=p;
}
}
for (int i = 0; i < accounts.size(); i++) {
for (int j = 1; j < accounts[i].size(); j++) {
unions[find(accounts[i][j], parents)].insert(accounts[i][j]);
}
}
vector<vector<string>> ans;
for (pair<string, set<string>> p : unions) {
vector<string> res(p.second.begin(), p.second.end());
res.insert(res.begin(), owner[p.first]);
ans.push_back(res);
}
return ans;
}
// ### Explanation with Example
// The code aims to merge email accounts by grouping them under common parent accounts using the **union-find** (also called **disjoint-set**) data structure. The process involves two main parts:
// 1. **Find operation**: Identifies the parent (or representative) for each email.
// 2. **Union operation**: Merges email accounts based on shared domains or owners.
// ### Step-by-Step Process:
// #### Input:
// Consider the following input accounts data:
// ```cpp
// vector<vector<string>> accounts = {
// {"John", "[email protected]", "[email protected]"},
// {"John", "[email protected]"},
// {"Mary", "[email protected]", "[email protected]"},
// {"Mary", "[email protected]"}
// };
// ```
// - Account 1: John has two emails: "[email protected]" and "[email protected]".
// - Account 2: John has an additional email: "[email protected]".
// - Account 3: Mary has two emails: "[email protected]" and "[email protected]".
// - Account 4: Mary has an additional email: "[email protected]".
// We want to merge accounts that have common emails. So the merged accounts should be:
// ### Code Walkthrough
// #### Step 1: Initialize Maps
// - `owner`: Tracks the owner of each email.
// - `parents`: Tracks the parent (or representative) of each email.
// - `unions`: Groups emails by their representative parent.
// #### Step 2: Set Initial Parent and Owner
// Each email starts as its own parent, and we store the account owner (name).
// ```cpp
// for(int i = 0; i < accounts.size(); i++) {
// for(int j = 1; j < accounts[i].size(); j++) {
// parents[accounts[i][j]] = accounts[i][j]; // email is its own parent initially
// owner[accounts[i][j]] = accounts[i][0]; // store the account owner's name
// }
// }
// ```
// This gives:
// - `parents`: {"[email protected]": "[email protected]", "[email protected]": "[email protected]", "[email protected]": "[email protected]", "[email protected]": "[email protected]", "[email protected]": "[email protected]"}
// - `owner`: {"[email protected]": "John", "[email protected]": "John", "[email protected]": "John", "[email protected]": "Mary", "[email protected]": "Mary"}
// #### Step 3: Union the Emails
// Next, we union emails that are part of the same account. The `find` function will identify the parent email, and we merge emails to the same parent.
// ```cpp
// for(int i = 0; i < accounts.size(); i++) {
// string p = find(accounts[i][1], parents); // find the parent of the first email
// for(int j = 2; j < accounts[i].size(); j++) {
// parents[find(accounts[i][j], parents)] = p; // merge subsequent emails under the same parent
// }
// }
// ```
// **For Account 1** ("John", "[email protected]", "[email protected]"):
// - Initially, "[email protected]" and "[email protected]" are their own parents.
// - After union, "[email protected]" will have the same parent as "[email protected]".
// **For Account 2** ("John", "[email protected]"):
// **For Account 3** ("Mary", "[email protected]", "[email protected]"):
// - "[email protected]" already has the same parent as "[email protected]".
// - After union, "[email protected]" will be grouped with "[email protected]".
// **For Account 4** ("Mary", "[email protected]"):
// - "[email protected]" will now be grouped with "[email protected]", and by extension, "[email protected]".
// After the union step, `parents` will look like this:
// - `parents`: {"[email protected]": "[email protected]", "[email protected]": "[email protected]", "[email protected]": "[email protected]", "[email protected]": "[email protected]", "[email protected]": "[email protected]"}
// #### Step 4: Group Emails by Parent
// We now group emails under their respective parent.
// ```cpp
// for(int i = 0; i < accounts.size(); i++) {
// for(int j = 1; j < accounts[i].size(); j++) {
// unions[find(accounts[i][j], parents)].insert(accounts[i][j]);
// }
// }
// ```
// This gives:
// - `unions`: {
// "[email protected]" -> {"[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"}
// }
// #### Step 5: Prepare the Final Result
// We now prepare the result by sorting the emails under each parent and adding the account owner's name at the front.
// ```cpp
// vector<vector<string>> ans;
// for (pair<string, set<string>> p : unions) {
// vector<string> res(p.second.begin(), p.second.end());
// res.insert(res.begin(), owner[p.first]); // add owner's name
// ans.push_back(res);
// }
// ```
// The final result is:
// ```cpp
// ans = {
// {"John", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"}
// }
// ```
// #### Step 6: Return the Final Merged Accounts
// ```cpp
// return ans;
// ```
// ### Final Output:
// ```cpp
// {
// }
// ```
// ### Conclusion:
// - Emails belonging to the same account (or person) are merged together.
// - The union-find algorithm helps group them under a common representative parent.
// - The output is a list of merged email accounts, each with the owner’s name and sorted emails.