Accounts Merge
MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.Each entry of accounts is [name, email1, email2, ...]. Two accounts belong to the same person if they share at least one email, and that can chain: if A shares an email with B and B shares one with C, all three are one person. Two people can have the same name, so the name alone proves nothing. Merge the accounts and return one entry per person: the name followed by that person's emails sorted, with no duplicates. The accounts themselves can be returned in any order.
accounts = [["Ada", "ada@x.com", "ada_work@x.com"],
["Ada", "ada@x.com", "ada_home@x.com"],
["Sam", "sam@x.com"],
["Ada", "ada2@x.com"]]
→ [["Ada", "ada@x.com", "ada_home@x.com", "ada_work@x.com"],
["Sam", "sam@x.com"],
["Ada", "ada2@x.com"]] (the last Ada shares no email, so she is someone else)Constraints
1 <= accounts.length <= 10002 <= accounts[i].length <= 10- Every email is a valid address of at most 30 characters, and every account with a shared email has the same name