4 line Python brute force solution


  • 0
    W

    Idea is simple that just make every word a set and compare every pair of sets. Compute quantity of interest if no intersection manifests.

    class Solution(object):
        def maxProduct(self, w):
            """
            :type words: List[str]
            :rtype: int
            """
            if not w or len(w)==1: return 0
            lw,ss,ls = len(w),[set(wi) for wi in w],[len(wi) for wi in w]
            r = [ls[i]*ls[j] for i in xrange(lw) for j in xrange(i+1,lw) if ss[i]-ss[j]==ss[i]]
            return 0 if not r else max(r)
    

Log in to reply
 

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