Account merge deduplication

Write codeMedium30 minMeta style

Your task

You're deduplicating user accounts. Each account is a list whose first element is a display name and remaining elements are emails. Two accounts belong to the same person if they share at least one email (this is transitive: AB and BC means A, B, C are the same person). Different people may share the same name. Return the number of distinct people after merging. There are at most 1000 accounts.

Examples

count_account_groups(accounts: list[list[str]]) -> int
Example 1
Inputcount_account_groups([["John", "a@x.com", "b@x.com"], ["John", "b@x.com"], ["Mary", "m@x.com"], ["John", "z@x.com"]])
Output3