Skip to content

Instantly share code, notes, and snippets.

@athursto
Last active February 22, 2024 20:34
Show Gist options
  • Save athursto/2a7751d39206f040a07fad23d518024d to your computer and use it in GitHub Desktop.
Save athursto/2a7751d39206f040a07fad23d518024d to your computer and use it in GitHub Desktop.
solution02.22.py
"""02.21.2024"""
"""
Given a string array, find the maximum product of word lengths where the words
don't share any letters.
"""
import itertools
list_input = ["fish","fear","boo","egg","cake","abcdef"]
def word_length_product(input):
comparisons = list(itertools.combinations(list_input, 2))
no_match = []
product = 0
for item in range(len(comparisons)):
comparison1 = set(comparisons[item][0])
comparison2 = set(comparisons[item][1])
if comparison1.intersection(comparison2):
print(f"match between {comparisons[item][0]} and {comparisons[item][1]}")
if not comparison1.intersection(comparison2):
no_match.append(comparisons[item])
product_holder = len(comparisons[item][0]) * len(comparisons[item][1])
if product_holder > product:
product = product_holder
print(f"No match between {comparisons[item][0]} and {comparisons[item][1]}, new product is {product}")
longest = comparisons[item]
return product, longest
word_length_product(list_input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment