Count Player Teams by Shared Letters
Problem statement
You are given an array players, where each string is the lowercase English name of one player.
Two players are directly connected when their names contain at least one common letter. A team is a connected component under this rule: players connected through other players belong to the same team even if their own names share no letter.
Return the number of teams.
Function
countPlayerTeams(players: String[]) → intExamples
Example 1
players = ["ab","bc","de","ef"]return = 2The first two names form one team through b. The last two form another through e.
Example 2
players = ["abc","cde","efg"]return = 1The first and second names share c, and the second and third share e. Transitivity puts all three players on one team.
Example 3
players = ["amy","bob","cyd"]return = 2Amy and Cyd share y. Bob has no letter in common with either of them.
Constraints
0 <= players.length <= 200000.- Every name has length between
1and100. - Names contain only lowercase English letters.
- The sum of all name lengths is at most
2000000.