Find Non-Overlapping String Pairs by Letter Sets
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Evaluates efficient character set representation using bit manipulation and frequency-based combinatorial counting.
You are given an array words containing strings made up of lowercase English letters. Your task is to determine how many distinct index pairs (i, j) exist where i < j and the two strings at those positions contain completely different letters—that is, no letter appears in both strings.
Each string in the array is considered independently by its position. If the same string appears multiple times, each occurrence is treated as a separate element. A string with no characters is considered to have no letters, making it compatible with every other string, including other empty strings.
Write a function that returns the total count of such non-overlapping pairs.
Constraints:
1 <= len(words) <= 20000 <= len(words[i]) <= 1000- Each
words[i]contains only lowercase English letters'a'through'z'
Examples
Example 1
Input: find_non_overlapping_pairs(['abc', 'def', 'bcd', 'xyz'])
Output: 4
Example 2
Input: find_non_overlapping_pairs(['a', '', 'b', 'a'])
Output: 5
Hints
Hint 1
Think about what information you actually need from each string—does the order or frequency of letters matter?
Hint 2
With only 26 possible letters, you can represent which letters appear in a string using a compact binary encoding.
Hint 3
Two strings share no letters if and only if the bitwise AND of their letter-set representations equals zero.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that only the presence of letters matters, not their frequency or order. Since there are exactly 26 lowercase English letters, we can represent each string's character set as a 26-bit integer where bit position k indicates whether letter chr(ord('a') + k) appears in the string.
For example:
"abc"maps to bits 0, 1, 2 set → binary0b111→ decimal7"xyz"maps to bits 23, 24, 25 set""(empty string) maps to0(no bits set)
Two strings share no common letters if and only if the bitwise AND of their bitmasks equals zero. This single bitwise operation replaces what would otherwise require set intersection or character-by-character comparison.
The algorithm proceeds in two phases:
-
Encoding Phase: Iterate through each word and compute its bitmask by setting the appropriate bit for each character that appears (duplicate characters simply set the same bit again).
-
Counting Phase: For every pair of indices
(i, j)wherei < j, test whethermasks[i] & masks[j] == 0. If true, increment the counter.
Empty strings automatically work correctly because their mask is 0, and 0 & x equals 0 for any value x, making empty strings compatible with all other strings including other empty strings.
Complexity
Time Complexity: O(L + n²)
- Building the masks requires O(L) time, where L is the total number of characters across all strings
- Checking all pairs requires O(n²) comparisons, where n is the number of words
- At the constraint limits (n = 2000, total length ≤ 2 million), this is efficient
Space Complexity: O(n)
- We store one integer mask per word
Solution Code
from typing import List
def find_non_overlapping_pairs(words: List[str]) -> int:
# Convert each word to a bitmask representing its character set
masks = []
for word in words:
mask = 0
for char in word:
mask |= 1 << (ord(char) - ord('a'))
masks.append(mask)
# Count pairs where the bitwise AND is zero (no shared letters)
count = 0
for i in range(len(masks)):
for j in range(i + 1, len(masks)):
if masks[i] & masks[j] == 0:
count += 1
return count