Accounts Merge Medium / #721
You get a list of accounts: each is a name followed by emails. Two accounts belong to one person when they share an email, also through other accounts; one name does not make one person. Merge the accounts of each person and return each person as the name followed by all their emails, sorted. Our example: ["Ann", "ann@home", "ann@work"], ["Ann", "a.k@uni", "ann@work"], ["Ann", "ak@mail", "a.k@uni"], ["Ann", "ann@x"] → ["Ann", "a.k@uni", "ak@mail", "ann@home", "ann@work"], ["Ann", "ann@x"].
input accounts: a list of accounts. accounts[i][0] is a name, and the rest of accounts[i] are emails. returns The accounts of each person merged into one. Two accounts belong to one person when they share an email, also through other accounts; the same name alone does not make one person. Each person is the name followed by all their emails, each once and in sorted order; the people in any order. constraints 1 ≤ accounts.length ≤ 50 2 ≤ accounts[i].length ≤ 10 accounts[i][0]: 1 to 10 English letters accounts[i][j], j ≥ 1: an email of at most 30 lowercase letters, digits and dots, with one @ accounts that share an email have the same name our 11 tests keep to them examples Open a test in the tests panel: it shows the input and the expected answer. walkthrough solve timer --:-- off
switch language → JavaScript TypeScript Python Go Java submit only: no test in the browserC++ submit only: no test in the browserother /**
* @param {string[][]} accounts
* @return {string[][]}
*/
function accountsMerge(accounts) {
}
test submit
11 tests · limit 3 s
01 — accounts = [["Ann", "ann@home", "ann@work"], ["Ann", "a.k@uni", "ann@work"], ["Ann", "ak@mail", "a.k@uni"], ["Ann", "ann@x"]] copy 02 — accounts = [["John", "john@a.com", "john@b.com"], ["John", "jo@c.com"]] copy 03 — accounts = [["Mia", "m1@x.com", "m2@x.com"], ["Mia", "m4@x.com"], ["Bob", "bob@y.com"], ["Mia", "m3@x.com", "m4@x.com"], ["Mia", "m2@x.com", "m3@x.com"]] copy 04 — accounts = [["Lee", "lee@z.com", "lee@z.com", "l@z.com"], ["Lee", "l@z.com", "lee@z.com"]] copy 05 — accounts = [["Kim", "kim@k.com"]] copy 06 — accounts = [["Bob", "bob2.2@mail.com", "bob2.1@mail.com", "bob2.3@mail.com", "bob2.0@mail.com"], ["Bob", "bob2.3@mail.com"], ["Ann", "ann0.1@mail.com", "ann0.0@mail.com", "ann0.2@mail.com"], ["Ann", "ann0.2@mail.com"], ["Ann", "ann3.0@mail.com"], ["Bob", "bob1.1@mail.com", "bob1.2@mail.com", "bob1.0@mail.com", "bob1.3@mail.com"], ["Bob", "bob2.0@mail.com"]] copy 07 — accounts = [["Eve", "eve4@x.com", "eve3@x.com"], ["Eve", "eve14@x.com", "eve15@x.com"], ["Eve", "eve6@x.com", "eve7@x.com"], ["Eve", "eve16@x.com", "eve17@x.com"], ["Eve", "eve12@x.com", "eve13@x.com"], ["Eve", "eve0@x.com", "eve1@x.com"], ["Eve", "eve3@x.com", "eve2@x.com"], ["Eve", "eve16@x.com", "eve15@x.com"], ["Eve", "eve4@x.com", "eve5@x.com"], ["Eve", "eve12@x.com", "eve11@x.com"], ["Eve", "eve1@x.com", "eve2@x.com"], ["Eve", "eve11@x.com", "eve10@x.com"], … 20 arrays] copy 08 — accounts = [["Leo", "leo0.0@mail.com", "leo0.0@mail.com", "leo0.0@mail.com"], ["John", "john2.0@mail.com", "john2.1@mail.com", "john2.2@mail.com"], ["John", "john2.4@mail.com", "john2.3@mail.com", "john2.0@mail.com", "john2.3@mail.com", "john2.4@mail.com"], ["John", "john1.4@mail.com", "john1.2@mail.com", "john1.5@mail.com"], ["John", "john1.2@mail.com", "john1.3@mail.com", "john1.0@mail.com", "john1.1@mail.com"], ["Leo", "leo0.0@mail.com", "leo0.1@mail.com", "leo0.2@mail.com"], ["Leo", "leo0.3@mail.com", "leo0.2@mail.com"]] copy 09 — accounts = [["Ann", "ann0.6@mail.com", "ann0.5@mail.com", "ann0.4@mail.com", "ann0.0@mail.com", "ann0.1@mail.com", "ann0.2@mail.com", "ann0.3@mail.com"], ["Leo", "leo5.0@mail.com", "leo5.1@mail.com"], ["John", "john3.0@mail.com"], ["Bob", "bob1.3@mail.com", "bob1.2@mail.com", "bob1.0@mail.com", "bob1.1@mail.com", "bob1.4@mail.com"], ["Mia", "mia2.0@mail.com", "mia2.1@mail.com", "mia2.2@mail.com"], ["Kate", "kate4.2@mail.com", "kate4.5@mail.com", "kate4.4@mail.com", "kate4.3@mail.com", "kate4.1@mail.com", "kate4.0@mail.com"]] copy 10 — accounts = [["Leo", "leo17@x.com", "leo16@x.com"], ["Leo", "leo1@x.com", "leo0@x.com"], ["Leo", "leo2@x.com", "leo1@x.com"], ["Leo", "leo19@x.com", "leo20@x.com"], ["Leo", "leo19@x.com", "leo18@x.com"], ["Leo", "leo4@x.com", "leo5@x.com"], ["Leo", "leo7@x.com", "leo6@x.com"], ["Leo", "leo2@x.com", "leo3@x.com"], ["Leo", "leo8@x.com", "leo7@x.com"], ["Leo", "leo15@x.com", "leo14@x.com"], ["Leo", "leo6@x.com", "leo5@x.com"], ["Leo", "leo10@x.com", "leo9@x.com"], … 50 arrays] copy 11 — accounts = [["Bob", "bob11.12@mail.com", "bob11.5@mail.com", "bob11.7@mail.com", "bob11.10@mail.com", "bob11.11@mail.com", "bob11.2@mail.com", "bob11.8@mail.com", "bob11.9@mail.com", "bob11.6@mail.com"], ["Bob", "bob11.0@mail.com", "bob11.4@mail.com", "bob11.3@mail.com", "bob11.1@mail.com", "bob11.2@mail.com"], ["Mia", "mia5.1@mail.com", "mia5.3@mail.com", "mia5.7@mail.com", "mia5.0@mail.com", "mia5.6@mail.com", "mia5.2@mail.com", "mia5.4@mail.com", "mia5.5@mail.com"], ["Ann", "ann6.0@mail.com"], ["Ann", "ann1.8@mail.com", "ann1.9@mail.com", "ann1.10@mail.com"], ["Mia", "mia9.0@mail.com", "mia9.5@mail.com", "mia9.4@mail.com", "mia9.8@mail.com", "mia9.2@mail.com", "mia9.7@mail.com", "mia9.3@mail.com", "mia9.6@mail.com"], ["Ann", "ann12.0@mail.com", "ann12.2@mail.com", "ann12.1@mail.com"], ["Bob", "bob10.1@mail.com", "bob10.0@mail.com", "bob10.2@mail.com"], ["Ann", "ann6.4@mail.com", "ann6.6@mail.com", "ann6.3@mail.com", "ann6.0@mail.com", "ann6.1@mail.com", "ann6.5@mail.com", "ann6.7@mail.com", "ann6.2@mail.com"], ["Mia", "mia2.0@mail.com"], ["Bob", "bob3.6@mail.com", "bob3.2@mail.com", "bob3.8@mail.com", "bob3.0@mail.com", "bob3.5@mail.com", "bob3.7@mail.com", "bob3.4@mail.com", "bob3.1@mail.com", "bob3.3@mail.com"], ["Bob", "bob0.0@mail.com", "bob0.1@mail.com", "bob0.3@mail.com", "bob0.2@mail.com", "bob0.4@mail.com"], … 50 arrays] copy
Stuck? See the solution run step by step on our example, then why it works and its complexity.
Watch the walkthrough → The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.