 All Problems
Accounts Merge
medium
union-find
hash table
string
amazon
google
facebook

Given a list of accounts where each element accounts[i] is a list of strings — the first element is the account name and the rest are emails — merge accounts that share at least one common email.

Return merged accounts sorted by email, with the name as the first element.

Example:

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

Constraints:

  • 1 <= accounts.length <= 1000
  • 2 <= accounts[i].length <= 10
Run to check your code against the sample cases, or submit to run every case