This article is for self education.
Refer : https://leetcode.com/problems/accounts-merge/solution/
Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.
Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.
After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.
Example 1:
Input: accounts = [["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: The first and second John's are the same person as they have the common email "johnsmith@mail.com". The third John and Mary are different people as none of their email addresses are used by other accounts. We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], ['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
Example 2:
Input: accounts = [["Gabe","Gabe0@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"]]
Constraints:
1 <= accounts.length <= 10002 <= accounts[i].length <= 101 <= accounts[i][j].length <= 30accounts[i][0]consists of English letters.accounts[i][j] (for j > 0)is a valid email.
Intuition
Here, we will represent emails as nodes, and an edge will signify that two emails are connected and hence belong to the same person. This means that any two emails that are connected by a path of edges must also belong to the same person. Initially, we are given accounts, where each account's emails make up a connected component.
Our first step should be to ensure that for each account, all of its nodes are connected. Suppose an account has emails, and we want to connect these emails. Since all emails in an account are connected, we can add an edge between every pair of emails. This will create a complete subgraph and require adding edges. However, do we really need that many edges to keep track of which emails belong to the same account? No, as long as two emails are connected by a path of edges, we know they belong to the same account. So instead of creating a complete subgraph for each account, we can create an acyclic graph using only edges. Recall that is the minimum number of edges required to connect nodes. In this approach, we will connect emails in an account in a star manner with the first email as the internal node of the star and all other emails as the leaves (as shown below).
List<List<String>> mergedAccountsList = new ArrayList<>();
Map<String, List<String>> adjacencyList = new HashMap<>();
HashSet<String> visited = new HashSet<>();
public List<List<String>> accountsMerge(List<List<String>> accountsList) {
//creating adjacency list
for (int i = 0; i < accountsList.size(); i++) {
List<String> account = accountsList.get(i);
String accountFirstEmail = account.get(1);
if (!adjacencyList.containsKey(accountFirstEmail)) {
adjacencyList.put(accountFirstEmail, new ArrayList<String>());
}
for (int j = 2; j < account.size(); j++) {
String email = account.get(j);
adjacencyList.get(accountFirstEmail).add(email);
if (!adjacencyList.containsKey(email)) {
adjacencyList.put(email, new ArrayList<String>());
}
adjacencyList.get(email).add(accountFirstEmail);
}
}
//Merging
for (int k = 0; k < accountsList.size(); k++) {
List<String> accountx = accountsList.get(k);
String actName = accountx.get(0);
String emailx = accountx.get(1);
if (!visited.contains(emailx)) {
List<String> mergeAccount = new ArrayList<>();
mergeAccount.add(actName);
dfs(mergeAccount, emailx);
Collections.sort(mergeAccount.subList(1, mergeAccount.size()));
mergedAccountsList.add(mergeAccount);
}
}
return mergedAccountsList;
}
private void dfs(List<String> mergedAccount, String email) {
if (!visited.contains(email)) {
mergedAccount.add(email);
visited.add(email);
}
if (!adjacencyList.containsKey(email)) {
return;
}
for (String emailx : adjacencyList.get(email)) {
if (!visited.contains(emailx)) {
dfs(mergedAccount, emailx);
}
}
}
No comments:
Post a Comment