Skip to content
BytePatterns

Word Pairs With No Shared Letters

MediumBit Manipulation#bitmask#set-as-integer~20m

Problem

A puzzle setter wants two clue words that share no letter at all, and the longer the pair the better. Given a list of lowercase words, return the largest value of len(a) * len(b) over two different words a and b that have no letter in common, or 0 if no such pair exists. There are up to 1,000 words of up to 1,000 letters each, so comparing the letters of every pair directly is too slow.

Examples

Input:  words = ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
Output: 16
Why:    "abcw" and "xtfn" share no letter, and 4 * 4 = 16
Input:  words = ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
Output: 4
Why:    "ab" and "cd" is the best disjoint pair
Input:  words = ["a", "aa", "aaa"]
Output: 0
Why:    edge case, every pair shares the letter a

Hints

0 / 3

Stuck on the idea rather than the code? Bitmask as a Set covers it.