Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 730 Bytes

318.最大单词长度乘积.md

File metadata and controls

20 lines (17 loc) · 730 Bytes

318.最大单词长度乘积

https://leetcode-cn.com/problems/maximum-product-of-word-lengths/

一、暴力

用双指针将所有字符串两两比较,转为set求交集判断是否有重合字符。最后记得用原串长度计算乘积(串中可能有重复字符)

class Solution:
    def maxProduct(self, words: List[str]) -> int:
        res = 0
        n = len(words)
        for i in range(n):
            for j in range(i+1, n):
                setI = set(words[i])
                setJ = set(words[j])
                if not (setI & setJ):
                    res = max(res, len(words[i])* len(words[j]))
        return res