边工作边刷题:70天一遍leetcode: day 53-1

Maximum Product of Word Lengths

class Solution(object):
    def maxProduct(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        es = []
        for w in words:
            es.append(sum(1 << (ord(c)-ord('a')) for c in set(w))) # error 1: not using set(w), wrong ["a","aa","aaa","aaaa"]
        
        maxLen = 0
        for i in xrange(len(words)-1):
            for j in xrange(i+1, len(words)):
                if not (es[i] & es[j]):
                    if maxLen<len(words[i])*len(words[j]):
                        maxLen = len(words[i])*len(words[j])
        
        return maxLen
            

posted @ 2016-06-20 04:24  absolute100  阅读(89)  评论(0编辑  收藏  举报