暴力求解没超时,那就这样吧
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