Accounts Merge

Given a list of accounts where each account contains a name followed by email addresses, merge accounts that belong to the same person if they share at least one email. DSU path compression is used to collapse email connectivity into a representative account.

Input Format

accounts = list of name plus email lists

Output Format

merged account lists

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

accounts = [["John","johnsmith@mail.com","john00@mail.com"],["John","johnnybravo@mail.com"],["John","johnsmith@mail.com","john_newyork@mail.com"],["Mary","mary@mail.com"]]

Output:

[[John,john00@mail.com,john_newyork@mail.com,johnsmith@mail.com],[John,johnnybravo@mail.com],[Mary,mary@mail.com]]

Explanation:

The two John accounts sharing johnsmith@mail.com are merged, and each merged account is sorted lexicographically.

Example 2:

Input:

accounts = [["Alex","a@mail.com"],["Alex","b@mail.com"],["Alex","a@mail.com","b@mail.com"]]

Output:

[[Alex,a@mail.com,b@mail.com]]

Explanation:

All Alex accounts are connected through shared emails.

Example 3:

Input:

accounts = [["Bob","bob@mail.com"]]

Output:

[[Bob,bob@mail.com]]

Explanation:

A single account remains unchanged.

Loading...
Accounts Merge - Union Find DSA Problem