Given a string array words, return the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. If no such two words exist, return 0.
Example 1:
Input: words = ["abcw","baz","foo","bar","xtfn","abcdef"] Output: 16 Explanation: The two words can be "abcw", "xtfn".
Example 2:
Input: words = ["a","ab","abc","d","cd","bcd","abcd"] Output: 4 Explanation: The two words can be "ab", "cd".
Example 3:
Input: words = ["a","aa","aaa","aaaa"] Output: 0 Explanation: No such pair of words.
Constraints:
2 <= words.length <= 10001 <= words[i].length <= 1000words[i] consists only of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find two words that don't share any letters and whose lengths, when multiplied, give the biggest result. The brute force way is to check all possible pairs of words to see if they fit the requirements.
Here's how the algorithm would work step-by-step:
def maximum_product_of_word_lengths_brute_force(words):
maximum_product = 0
for first_word_index in range(len(words)):
first_word = words[first_word_index]
for second_word_index in range(len(words)):
second_word = words[second_word_index]
# Avoid comparing a word to itself
if first_word_index == second_word_index:
continue
share_common_letters = False
for letter in first_word:
if letter in second_word:
# If they share common letters, mark it down
share_common_letters = True
break
if not share_common_letters:
# Only calculate product if no shared letters
product = len(first_word) * len(second_word)
# Track the largest product seen so far
if product > maximum_product:
maximum_product = product
return maximum_productThe goal is to find two words that don't share any letters and whose lengths multiplied together give the largest possible product. Instead of directly comparing each pair of words to every other word, we'll use a clever way to represent each word's letters to speed up the process.
Here's how the algorithm would work step-by-step:
def maximum_product_of_word_lengths(words):
word_representations = []
for word in words:
representation = 0
for char in word:
representation |= (1 << (ord(char) - ord('a')))
word_representations.append(representation)
max_product = 0
for i in range(len(words)):
for j in range(i + 1, len(words)):
# Check if the two words have common letters.
if (word_representations[i] & word_representations[j]) == 0:
current_product = len(words[i]) * len(words[j])
# Track the maximum product found so far.
max_product = max(max_product, current_product)
return max_product| Case | How to Handle |
|---|---|
| Empty input array | Return 0 immediately as no product is possible. |
| Array containing only empty strings | Return 0 because the product of any lengths with a 0 is 0. |
| Array containing a single word | Return 0 because you need at least two words to form a product. |
| All words in the array share at least one common character. | The maximum product should be 0 since no disjoint words are available. |
| Very long words causing integer overflow of the product. | Use a 64-bit integer type or appropriate overflow handling to prevent incorrect results. |
| Maximum number of words in the input array. | Ensure the solution's time complexity remains acceptable when approaching the maximum word count. |
| Words with non-ASCII characters. | Handle unicode characters correctly when creating bitmasks or checking for common characters; a bitmask may not be sufficient. |
| Words with the same set of characters but different ordering. | The bitmask or other character-set representation should handle this correctly, preventing such words from being considered disjoint. |