https://leetcode.cn/problems/maximum-product-of-word-lengths/
给你一个字符串数组 words ,找出并返回 length(words[i]) * length(words[j]) 的最大值,并且这两个单词不含有公共字母。如果不存在这样的两个单词,返回 0 。
示例 1:
输入:words = [“abcw”,“baz”,“foo”,“bar”,“xtfn”,“abcdef”]
输出:16
解释:这两个单词为 “abcw”, “xtfn”。
class Solution:
def maxProduct(self, words: List[str]) -> int:
# 难度:☆☆
# 标签:数组、字符串
# intersection()方法返回两个或更多集合中都包含的元素(交集),以集合形式返回
lst = [set(s) for s in words]
result = 0
for i in range(len(words)):
for j in range(i + 1, len(words)):
if not lst[i].intersection(lst[j]):
result = max(result, len(words[i]) * len(words[j]))
return result