Disjoint String Pairs
MediumStrings · Netflix
Given a list of lowercase words words, return the number of index pairs (i, j) with i < j such that words[i] and words[j] share no letter at all.
Pairs are counted by index, so equal words at different positions are different words (although two identical non-empty words always share a letter).
Example 1
- Input:
- words = ["moon", "star", "sun", "sky"]
- Output:
- 2
- Explanation
moonshares nothing withstarorsky. Every other pair has a common letter:sunhasnlikemoonandslike the rest, andstarandskyshares. That gives 2 pairs.
Example 2
- Input:
- words = ["aa", "a", "b"]
- Output:
- 2
- Explanation
aaandbshare nothing, and neither doaandb. Repeated letters inside a word do not matter, andaaandasharea.
Example 3
- Input:
- words = ["abc", "cab"]
- Output:
- 0
- Explanation
- Both words use the same letters.
Constraints
1 <= words.length <= 20001 <= words[i].length <= 1000words[i]contains only lowercase English letters