Skip to content
DeepForgeby svx
All problems
HardData Structuresds-085

Accounts Merge Count

Problem

Count how many groups of accounts remain after merging accounts that share an email address.

accounts is a list of [name, email, ...] entries. Two accounts belong to the same person when they share at least one email address; union their indices and count the distinct roots. Accounts with the same name but no shared email stay separate.

Return the number of merged groups.

Hint

Map each email to the first account that owns it; a repeat email unions the two accounts.

Test cases

case 1
in: [[["John","a@x","b@x"],["John","b@x","c@x"],["Mary","m@x"]]]
expected: 2
case 2
in: [[["A","e1"],["B","e2"]]]
expected: 2
case 3
in: [[["A","x","y"],["A","y","z"],["A","z","w"]]]
expected: 1
case 4
in: [[]]
expected: 0
case 5
in: [[["A","x"],["A","x"]]]
expected: 1
Starter codePython
def accounts_merge_count(accounts):
    # Your code here
    pass

5 test cases · run your code against them in the editor