Solution in python

  • 0
    def maxProduct(self, words):
        :type words: List[str]
        :rtype: int
        nums = {}
        keys =[]
        size = len(words)
        for word in words:
            num = sum(1 << (ord(x) - ord('a')) for x in set(word))
            if num in keys and len(word) > len(nums[num]):
                nums[num] = word
            if num not in keys:
                nums[num] = word
        ans = 0
        for x in range(len(keys)):
            for y in range(x+1, len(keys)):
                if not keys[x] & keys[y]:
                    ans = max(len(nums[keys[x]]) * len(nums[keys[y]]), ans)
        return ans

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.