Accounts Merge
Each account is a list containing a person's name followed by one or more email addresses. Accounts sharing an email belong to the same person, including…
Each account is a list containing a person's name followed by one or more email addresses. Accounts sharing an email belong to the same person, including through a chain of shared emails. Merge each connected group into one row: the name followed by its distinct emails in ascending Unicode scalar-value order. Return the rows in any order. Equal names alone do not imply identity. Accounts connected through email always have the same name.
- 1 <= accounts.length <= 1000. - 2 <= accounts[i].length <= 15, including the name. - Each string contains 1..30 Unicode scalar values; names contain English letters only. - Email identity is exact: do not normalize, case-fold, or restrict emails to ASCII. - Merged rows may contain more than 14 emails; only input rows have that limit. - Visualization inputs contain at most six accounts and four emails per account.
Example 1: input accounts = [["Ada","a","z0"],["Ada","a","x1"],["Ada","b","z2"],["Ada","b","x3"],["Ada","a","b"]]; output [["Ada","a","b","x1","x3","z0","z2"]]. Accounts 0 and 1 share a, while accounts 2 and 3 share b. Account 4 contains both emails and joins those earlier components. Every distinct email appears once in the sorted suffix.
Example 2: input accounts = [["Ada","a"],["Ada","b"]]; output [["Ada","a"],["Ada","b"]]. The names match, but no shared email connects these accounts. They remain separate people.
Category: Graphs. Topic: graph. Difficulty: Hard. Patterns: union-find.