最后更新于3年前
用双指针将所有字符串两两比较,转为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