Straightforward Python Solution


  • 0
    W
    from itertools import combinations
    
    class Solution(object):
        def maxProduct(self, words):
            ws = [(set(w), len(w)) for w in words]
            ret = 0
            for (s1, l1), (s2, l2) in combinations(ws, 2):
                if not (s1 & s2):
                    ret = max(ret, l1 * l2)
            return ret
    

    the idea is to pre-compute set and len for each word to avoid duplicate computations


Log in to reply
 

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