Python Solution O(n^2)


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

Log in to reply
 

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