leetcode318. Maximum Product of Word Lengths

来源:互联网 发布:指南针炒股软件诈骗: 编辑:程序博客网 时间:2024/05/29 19:54

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
Example 1:
Given [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”]
Return 16
The two words can be “abcw”, “xtfn”.
Example 2:
Given [“a”, “ab”, “abc”, “d”, “cd”, “bcd”, “abcd”]
Return 4
The two words can be “ab”, “cd”.
Example 3:
Given [“a”, “aa”, “aaa”, “aaaa”]
Return 0
No such pair of words.

用26个字母来标记
a->0
b->1
将单词转化为二进制
算法复杂度O(n^2)

class Solution(object):    def maxProduct(self, words):        """        :type words: List[str]        :rtype: int        """        ans=[]        for i in words:            tmp=0            for j in set(i):                tmp=tmp+(1<<(ord(j)-ord('a')))            ans.append(tmp)        MAX_P=0        for i in range(len(words)):            for j in range(i,len(words)):                if ans[i] & ans[j]==0:                    MAX_P=max(MAX_P,len(words[i])*len(words[j]))        return int(MAX_P)
0 0
原创粉丝点击