Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Accounts Merge (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

You are given a list of accounts, each of which is a list of strings. The first string in each list is the account name, and the rest are email addresses associated with that account.

You are required to merge accounts that belong to the same person. Two accounts are considered to belong to the same person if there is at least one common email address between them.

Note that even if two accounts have the same name, they may belong to different people, as people could have the same name.

After merging, the output should be a list of accounts in which each account is represented by a list containing the name and the unique email addresses sorted in alphabetical order. The order of accounts in the output does not matter.

Examples

Example 1:

Input:

accounts = [["Alice", "alice@mail.com", "alice123@mail.com"],
  ["Alice", "alice123@mail.com", "alice_newyork@mail.com"],
  ["Bob", "bob@mail.com"],
  ["Alice", "alice@mail.com", "alice00@mail.com"]
]

Expected Output:

[
  ["Alice", "alice@mail.com", "alice00@mail.com", "alice123@mail.com", "alice_newyork@mail.com"],
  ["Bob", "bob@mail.com"]
]

Justification: The first, second, and fourth accounts belong to Alice and have overlapping emails. Hence, they are merged into one account.

Example 2:

Input:

accounts = [["Charlie", "charlie@mail.com", "charlie00@mail.com"],
  ["Charlie", "charlie_newyork@mail.com", "charlie00@mail.com"],
  ["Eve", "eve@mail.com"],
  ["Bob", "bob@mail.com"]
]

Expected Output:

[
  [["Charlie","charlie00@mail.com","charlie@mail.com","charlie_newyork@mail.com"],
   ["Eve","eve@mail.com"], 
   ["Bob","bob@mail.com"]]
]

Justification: The first, and second accounts belong to Charlie and have overlapping emails. Hence, they are merged into one account.

Example 3:

Input:

accounts = [["David", "david@mail.com"],
  ["David", "david1@mail.com", "david123@mail.com"],
  ["John", "john@mail.com"],
  ["David", "david123@mail.com"]
]

Expected Output:

[
  ["David","david@mail.com"],
  ["David","david123@mail.com","david1@mail.com"],
  ["John","john@mail.com"]]
]

Justification: The second, and fourth accounts belong to David and have overlapping emails. The first account has name equal to David but it doesn't have overlapping email with second and fourth account.

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] (for j > 0) is a valid email.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible