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

• 标签：位运算、数组、字符串
• 难度：中等

代码 #

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  class Solution: def maxProduct(self, words: List[str]) -> int: size = len(words) arr = [0 for _ in range(size)] for i in range(size): word = words[i] len_word = len(word) for j in range(len_word): arr[i] |= 1 << (ord(word[j]) - ord('a')) ans = 0 for i in range(size): for j in range(i + 1, size): if arr[i] & arr[j] == 0: k = len(words[i]) * len(words[j]) ans = k if ans < k else ans return ans