Problem · Array

Count String Pairs With Disjoint Characters

Learn this problem
MediumNetflix logoNetflixFULLTIMEPHONE SCREEN

Problem statement

Given an array of strings words, return the number of unordered index pairs (i, j) such that i < j and the two strings share no character.

For this exercise, assume every array index is a distinct candidate, even when two strings have equal values. Every string contains only lowercase English letters and may be empty. An empty string shares no character with any string.

Function

countDisjointCharacterPairs(words: String[]) → int

Examples

Example 1

words = ["abc","def","ad","xyz"]return = 4

The valid pairs are (0,1), (0,3), (1,3), and (2,3).

Example 2

words = ["a","a","b",""]return = 5

The two equal a values occupy different indices. Each pairs with b and the empty string, while b also pairs with the empty string.

Example 3

words = []return = 0

An empty array contains no index pair.

Constraints

  • 0 <= words.length <= 5000
  • 0 <= sum(words[i].length) <= 200000
  • Every string contains only lowercase English letters and may be empty.
  • Each array index is counted as a distinct candidate.

More Netflix problems

drafts saved locally
public int countDisjointCharacterPairs(String[] words) {
  // write your code here
}
words["abc","def","ad","xyz"]
expected4
checking account