![]() |
VOOZH | about |
Given a 2D array arr[][], where each element arr[i] is an array of strings representing an account, where the first element arr[i][0] is a name, and the rest of the elements are emails associated with this name. Also, two accounts belong to the same person if there is a common email associated with both accounts. A person can have any number of accounts initially, but all have the same name.
The task is to merge these accounts and print them in the following format: the first element of each account is the name, and the rest of the elements are emails in lexicographically sorted order.
Note: Accounts themselves can be returned in any order. Even if two accounts have the same name, they may belong to different people as different people could have the same name.
Example:
Input: arr[][] =
[["John", "johnsmith@mail.com", "john_newyork@mail.com"],
["John", "johnsmith@mail.com", "john00@mail.com"],
["Mary", "mary@mail.com"],
["John", "johnnybravo@mail.com"]]
Output:
[["John", "john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com"],["Mary","mary@mail.com"],
["John","johnnybravo@mail.com"]]
Explanation: There are three accounts with the same name "John", and two of them share a common email which is "johnsmith@mail.com", thus these two accounts are merged, and remaining two will remain unchanged.Input: arr[][] =
[["Gabe", "Gabe00@m.co", "Gabe3@m.co", "Gabe1@m.co"],["Kevin","Kevin3@m.co","Kevin5@m.co", "Kevin0@m.co"], ["Ethan","Ethan5@m.co","Ethan4@m.co","Ethan0@m.co"],["Hanzo","Hanzo3@m.co","Hanzo1@m.co","Hanzo0@m.co"],["Fern","Fern5@m.co","Fern1@m.co","Fern0@m.co"]]
Output:
[["Ethan","Ethan0@m.co","Ethan4@m.co","Ethan5@m.co"],["Gabe","Gabe0@m.co","Gabe1@m.co","Gabe3@m.co"],["Hanzo","Hanzo0@m.co","Hanzo1@m.co","Hanzo3@m.co"],["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"],["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"]]
Explanation: There is no common email in any of the users.
The idea is to represent the list of accounts arr[][] in the form of Graph, where each node represents an email, and an edge signifies that two emails are connected i.e. they belong to same person. To do so, firstly connect all the emails initially associated to the same name in Star Graph format.
Now explore each connected component to find all the emails that belong to one person using DFS. To do so, iterate over all of the nodes and if the node has already been visited, skip it. Otherwise, perform a DFS traversal over the connected component and store all the visited emails together, as they all belong to one person. Each time we visit an email during a DFS, we will mark it as visited to ensure that we do not search the same connected component more than once.
John john00@mail.com john_newyork@mail.com johnsmith@mail.com Mary mary@mail.com John johnnybravo@mail.com
Time Complexity: O(n * k * log(n * k)), where n * k is total number of emails.
Space Complexity: O(n * k), to build an adjacency list.
The idea is to iterate through the given accounts, and for each email encountered, either add it to an existing set (if the email is already encountered) or creates a new set. We will use Disjoint Set Union (DSU) to group emails that belong to the same account. After grouping emails, construct the final result by combining the emails within each set with the account name.
John john00@mail.com john_newyork@mail.com johnsmith@mail.com Mary mary@mail.com John johnnybravo@mail.com
Time Complexity: O(n * k * log(n * k)), where n * k is total number of emails.
Space Complexity: O(n * k), to build an adjacency list.