FastPrepCount Player Teams by Shared Letters

Count Player Teams by Shared Letters

Google logoGoogle● MediumINTERNONSITE INTERVIEW
Learn

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[]) → int

Examples

Example 1

players = ["ab","bc","de","ef"]return = 2

The first two names form one team through b. The last two form another through e.

Example 2

players = ["abc","cde","efg"]return = 1

The 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 = 2

Amy and Cyd share y. Bob has no letter in common with either of them.

Constraints

  • 0 <= players.length <= 200000.
  • Every name has length between 1 and 100.
  • Names contain only lowercase English letters.
  • The sum of all name lengths is at most 2000000.

More Google problems

See Google hiring insights
public int countPlayerTeams(String[] players) {
    // Write your code here.
}
players["ab","bc","de","ef"]
expected2
Checking account…