Accounts Merge Problem
Accounts Merge Problem — ExecCode Medium DSA Practice
Solve the Accounts Merge problem on ExecCode. Free online medium DSA practice in DSU. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given a list of accounts where each accounts[i] is a list of strings with the name as the first element and emails afterward, merge accounts that share at least one email (transitively). Return merged accounts as [name, ...emailsSorted]. Order of account rows may vary; emails within a row must be sorted ascending.
Examples
Input accounts = [["John", "johnsmith@mail.com", "johnnewyork@mail.com"], ["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "john00@mail.com", "johnny@mail.com"], ["Mary", "mary@mail.com"], ["John", "johnnybravo@mail.com"]]; Output [["John", "john00@mail.com", "johnnewyork@mail.com", "johnny@mail.com", "johnsmith@mail.com"], ["Mary", "mary@mail.com"], ["John", "johnnybravo@mail.com"]]. Input accounts = [["Alice", "a@x.com"], ["Bob", "b@x.com"]]; Output [["Alice", "a@x.com"], ["Bob", "b@x.com"]]
Constraints
1 ≤ accounts.length ≤ 1000 2 ≤ accounts[i].length ≤ 10 1 ≤ accounts[i][j].length ≤ 30 accounts[i][0] consists of English letters accounts[i][j] (j > 0) are emails
Practice Accounts Merge free on ExecCode. Browse DSA problems, topic map, and placement guides.