Skip to content

Latest commit

 

History

History
24 lines (22 loc) · 662 Bytes

318.md

File metadata and controls

24 lines (22 loc) · 662 Bytes

318. Maximum Product of Word Lengths

Solution 1 (time O(n^2), space O(n))

class Solution(object):
    def maxProduct(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        mask = []
        for word in words:
            cur_mask = 0
            for c in word:
                cur_mask |= (1 << (ord(c) - ord('a')))
            mask.append(cur_mask)
        ans = 0
        for i in range(len(mask)):
            for j in range(i + 1, len(mask)):
                if mask[i] & mask[j] == 0:
                    ans = max(ans, len(words[i]) * len(words[j]))
        return ans