


Input: 'n' = 4,
'accounts' = [
["Rohan", "rohan123@gmail.com", "1279ro@gmail.com"],
["Rohit", "rohit101@yahoo.com", "hitman30487@gmail.com"],
["Rohan", "1279ro@gmail.com", "niemann01@gmail.com"],
["Rohan", "kaushik@outlook.com"],
]
Output: [
["Rohan", "rohan123@gmail.com", "1279ro@gmail.com", "niemann01@gmail.com"],
["Rohit", "rohit101@yahoo.com", "hitman30487@gmail.com"],
["Rohan", "kaushik@outlook.com"],
]
Explanation: The first and third "Rohan" are the same person as they have a shared email address, “1279ro@gmail.com”. The rest of the accounts are of different persons, as they don’t share any shared email addresses. So, we merge the first and third accounts.
The first input line contains an integer ‘n’ denoting the number of accounts.
Each of the next ‘n’ lines contains the length of 'accounts[i]' followed by space-separated strings where the first string denotes the account holder’s name, and the rest of the strings represent the email addresses.
In the first line, print the number of accounts after merging.
Print the space-separated name and email address associated with each account, each in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
The basic idea of this approach is to model this problem as a graph problem. Let us create a graph of emails with an edge between two emails if they occur in the same account.
The figure below shows the graph constructed for the first test case of sample test case 1.
We can observe that the set of accounts which needs to be merged will be a part of connected components. So the problem is now reduced to finding the connected components in an undirected graph. We can easily get the connected component by doing a depth-first search of the graph. You can refer to this tutorial of cp-algorithms for a more detailed explanation. Search for connected components in a graph - https://cp-algorithms.com/graph/search-for-connected-components.html#:~:text=Given%20an%20undirected%20graph%20G,path%20exists%20between%20different%20groups.
Let unordered_map<string, string> ‘EMAIL_TO_NAME’ be HashMap to store the mapping of emails to the name of the person having it. This mapping will help us in getting the name of the person while creating the merged accounts.
And also let unorderd_map<string, vector<string>> ‘GRAPH’ stores the adjacency list of the constructed graph.
Now the consider the following steps :
The basic idea of this approach is to use a Disjoint Set Union data structure to solve this task. We have seen in approach 1 that the problem is finally reduced to finding the connected component in an undirected graph. So we can use a DSU to do this task very efficiently. You can refer to this article from cp-algorithms to read more about DSU. https://cp-algorithms.com/data_structures/disjoint_set_union.html
We will use the union by size for optimization.
Let us create an unorderd_map<string, int> ‘EMAIL_TO_ID’ a HashMap to store the mapping of emails to an integer. It will help us in using Disjoint Set Union.
Let unordered_map<string, string> "EMAIL_TO_NAME' be HashMap to store the mapping of emails to the name of the person having it. This mapping will help us in getting the name of the person while creating the merged accounts.
Let us also create an unordered_map<string, vector<string>> ‘COMPS’ to store the mapping of a person’s name and the email addresses belonging to him.
Now consider the following steps: