每日一题 318. 最大单词长度乘积(中等)

在这里插入图片描述
暴力求解没超时,那就这样吧

class Solution:
    def maxProduct(self, words: List[str]) -> int:
        ans = 0
        for i in range(len(words)):
            for j in range(i + 1, len(words)):
                if len(words[i]) * len(words[j]) < ans:
                    continue
                t = 0
                for k in range(26):
                    ch = chr(k + ord('a'))
                    if ch in words[i] and ch in words[j]:
                        t = 1
                        break
                if t == 0:
                    ans = len(words[i]) * len(words[j])
        return ans

你可能感兴趣的:(用Python刷力扣,算法,leetcode,python)