Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
Example
Input: [“abcw”,“baz”,“foo”,“bar”,“xtfn”,“abcdef”]
Output: 16
Explanation: The two words can be “abcw”, “xtfn”.
Solution
class Solution:
def maxProduct(self, words: List[str]) -> int:
res = 0
for i in range(len(words)):
for j in range(i + 1, len(words)):
if not (set(words[i]) & set(words[j])):
res = max(res, len(words[i]) * len(words[j]))
return res