Maximum Product of Word Lengths - Problem
Given an array of unique strings, find the maximum product of lengths of two words that share no common letters.
Your task is to identify pairs of words where:
- No character appears in both words
- The product of their lengths is maximized
For example, given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"], the words "abcw" and "xtfn" share no common letters, so their product is 4 ร 4 = 16.
If no such pair exists, return 0.
Input & Output
example_1.py โ Basic Case
$
Input:
["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
โบ
Output:
16
๐ก Note:
The two words "abcw" and "xtfn" share no common letters, and their length product is 4 ร 4 = 16, which is the maximum possible.
example_2.py โ No Valid Pairs
$
Input:
["a", "aa", "aaa", "aaaa"]
โบ
Output:
0
๐ก Note:
All words contain the letter 'a', so no two words can be found that don't share common letters.
example_3.py โ Single Characters
$
Input:
["a", "b", "c", "d"]
โบ
Output:
1
๐ก Note:
Any two single-character words with different letters will work. The maximum product is 1 ร 1 = 1.
Constraints
- 2 โค words.length โค 1000
- 1 โค words[i].length โค 1000
- words[i] consists only of lowercase English letters
- All strings in words are unique
Visualization
Tap to expand
Understanding the Visualization
1
Agent Recruitment
Each agent brings a codebook with unique letters
2
Compatibility Check
Two agents are compatible if their alphabets don't overlap
3
Mission Capacity
Mission success depends on total vocabulary size (product of lengths)
4
Optimal Pairing
Find the compatible pair with maximum combined capacity
Key Takeaway
๐ฏ Key Insight: Bitmasks transform character set comparisons from O(k) set operations to O(1) bitwise operations, making pair checking extremely efficient for small alphabets.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code