6. Accounts Merge

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"]]

Solution: (union find)

Approach similar to smallest string with swaps

class Solution
{
public:
    int findPar(int a, vector<int> &par)
    {

        if (par[a] == -1)
        {
            return a;
        }

        par[a] = findPar(par[a], par);
        return par[a];
    }

    void uni(int a, int b, vector<int> &rank, vector<int> &par)
    {
        int parA = findPar(a, par);
        int parB = findPar(b, par);

        if (parA != parB)
        {
            if (rank[parA] > rank[parB])
            {
                par[parB] = parA;
            }
            else if (rank[parB] > rank[parA])
            {
                par[parA] = parB;
            }
            else
            {
                par[parA] = parB;
                rank[parB]++;
            }
        }

        return;
    }

    vector<vector<string>> accountsMerge(vector<vector<string>> &accounts)
    {

        unordered_map<string, int> m;
        int n = accounts.size();
        vector<int> par(n, -1);
        vector<int> rank(n, 0);

        for (int i = 0; i < accounts.size(); i++)
        {

            for (int j = 1; j < accounts[i].size(); j++)
            {
                if (m.find(accounts[i][j])!=m.end())
                {
                    uni(m[accounts[i][j]], i, rank, par);
                }
                else
                {
                    m[accounts[i][j]] = i;
                }
            }
        }

        unordered_map<int, vector<string>> k;

        for (auto itr = m.begin(); itr != m.end(); itr++)
        {
            k[findPar(itr->second, par)].push_back(itr->first);
        }

        vector<vector<string>> res;

        for (auto itr = k.begin(); itr != k.end(); itr++)
        {
            vector<string> emails = itr->second;
            int idx = m[emails[0]];
            sort(emails.begin(), emails.end());
            emails.insert(emails.begin(), accounts[idx][0]);
            res.push_back(emails);
        }

        return res;
    }
};

Last updated