AceYourInterviews
Checking sign-in…

111. Accounts Merge

CoreJoin account records through exact shared-email ownership, then canonicalize output

Merge records when they share an email address, including records connected indirectly through other records. Each connected account component becomes one output row.

A shared name alone is not enough to merge accounts. Deduplicate emails, follow the naming-conflict rules, and return independent mutable output rows.

Example 1

Input

[[A,z,x],[A,y,x],[A],[A,k]]

Output

[[A,x,y,z],[A],[A,k]]

Explanation: The first two records share x. Equal name A does not merge the independent name-only or k records.

Example 2

Input

[[Ada,a,b],[Ada,c,d],[Ada,b,c]]

Output

[[Ada,a,b,c,d]]

Explanation: The third record bridges two earlier groups. The final DSU root need not be source index zero.

Example 3

Input

[[Ada,x],[Grace,x]]

Output

IllegalArgumentException

Explanation: A shared exact email with conflicting names violates the declared identity policy.

Example 4

Input

[[A,X@example],[A,x@example]]

Output

two independent rows

Explanation: Identifier matching is case-sensitive; no email-provider normalization is inferred.

Constraints

  • 0 <= accounts.size <= 105
  • 0 <= email occurrences <= 2 * 105
  • 0 <= total UTF-16 code units <= 2 * 106
  • Each account begins with a nonempty name. Email identifiers are nonempty strings.
Hints
Hint 1

Treat records as vertices and each exact shared email as a connection.

Hint 2

Remember one first owner per email, checking the declared name-consistency policy.

Hint 3

After all unions, group distinct emails by final roots and emit components by earliest source index.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Shared email joins records; equal names alone do not

[["A","z","x"],["A","y","x"],["A"],["A","k"]]
[["A","x","y","z"],["A"],["A","k"]]