← All questions
MediumCoding

Accounts merge

Asked atGoogle
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Given a list of accounts where each element account [i] is a list of strings, where the first element account [i][0] is a name, and the rest of the elements are emails representing emails of the account.

Now, 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.

Input: N = 4, 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 the second John are the same person as they have a common email. But the third Mary and fourth John are not the same as they do not have any common email. The result can be in any order but the emails must be in sorted order. The following is also a valid result: [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], ['John', 'john00@mail.com' , 'john_newyork@mail.com', 'johnsmith@mail.com' ]]

Input: N = 6, accounts = [["John","j1@com","j2@com","j3@com"], ["John","j4@com"], ["Raj",”r1@com”, “r2@com”], ["John","j1@com","j5@com"], ["Raj",”r2@com”, “r3@com”], ["Mary","m1@com"]]

Output: [["John","j1@com","j2@com","j3@com","j5@com"], ["John","j4@com"], ["Raj",”r1@com”, “r2@com”, “r3@com”], ["Mary","m1@com"]]

Explanation: The first and the fourth John are the same person here as they have a common email. And the third and the fifth Raj are also the same person. So, the same accounts are merged.

Input: N = 3,

accounts = [ ["Alice", "alice@mail.com", "alice_work@mail.com"], ["Bob", "bob@gmail.com"], ["Alice", "alice_personal@mail.com", "alice@mail.com"] ]

· 1 <= N <= 1000 · 2 <= accounts[i].size <= 15 · 1 <= accounts[i][j].size <= 30 · accounts[i][0] consists of English letters.

cpp

class Solution{
public:
    vector<vector<string>> accountsMerge(vector<vector<string>> accounts){
        
    }
};

java

class Solution {
    static List<List<String>> accountsMerge(List<List<String>> accounts) {
       
    }
}

python

class Solution:
    def accountsMerge(self, accounts):

javascript

class Solution {
    accountsMerge(details) {
     
    }
}

csharp

public class Solution {
    public List<List<string>> AccountsMerge(List<List<string>> accounts) {

    }
}

go

func accountsMerge(details [][]string) [][]string {

}
Solve on LeetCode →
Stuck? Show a way to structure it+
  1. 01Map each email to an account index.
  2. 02Union accounts that share an email.
  3. 03Group emails by final representative.
  4. 04Sort emails inside each group.
  5. 05Prefix the group with that component's name.

Reference answer

Then expect these follow-ups

  • How would a DFS email graph solution compare?

    Tests: follow-up reasoning

  • How do you handle an account containing only one email?

    Tests: follow-up reasoning

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question