Skip to main content

🟡 剑指 Offer II 005. 单词长度的最大乘积

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

解析 index.md#

如何记录单词出现过的字母,这里转换成26位的二进制数字即可。

题解 1.py#

class Solution:    def maxProduct(self, words: List[str]) -> int:        def parseBin(word):            code = ['0'] * 26            for w in word:                code[ord(w)-ord('a')] = '1'            return int(''.join(code), 2)                wordCode = [parseBin(w) for w in words]        wordLeng = [len(w) for w in words]
        maxx = 0        for i in range(len(words)-1):            for j in range(i, len(words)):                if wordCode[i] & wordCode[j] == 0:                    maxx = max(maxx, wordLeng[i] * wordLeng[j])                return maxx