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.

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

class Solution(object):
def maxProduct(self, words):
bits_words = [reduce(lambda s,x: s|(1<<ord(x)-ord('a')), w, 0) for w in words]
ans = 0
for i, word in enumerate(words):
for j in range(i+1, len(words)):
if len(word)*len(words[j]) > ans and self.has_diff(bits_words[i], bits_words[j]):
ans = len(word)*len(words[j])
return ans def has_diff(self, w1, w2):
return (w1&w2) == 0

318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示的更多相关文章

  1. leetcode 318. Maximum Product of Word Lengths

    传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...

  2. LeetCode 【318. Maximum Product of Word Lengths】

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  3. 318. Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  4. Java [Leetcode 318]Maximum Product of Word Lengths

    题目描述: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where ...

  5. leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)

    https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...

  6. [leetcode]318. Maximum Product of Word Lengths单词长度最大乘积

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  7. [LC] 318. Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  8. 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...

  9. 318 Maximum Product of Word Lengths 最大单词长度乘积

    给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...

随机推荐

  1. WdatePicker.js 日历点击时,触发自定义方法 ,可以调用自己的函数。

    问题: 在选择日期后,没有提交按钮,得到日期后,就可以把日期传到后台,然后就可以得到数据. 方法: 在input 标签中加入onfocus ,就可以了. wdatePicker();可以自定义事件函数 ...

  2. iOS - NetRequest 网络数据请求

    1.网络请求 1.1 网络通讯三要素 1.IP 地址(主机名): 网络中设备的唯一标示.不易记忆,可以用主机名(域名). 1) IP V4: 0~255.0~255.0~255.0~255 ,共有 2 ...

  3. Getuserpassword

    将[新注册的用户的用户名和密码]保存到服务端本地 /*将注册成功的用户名和密码保存到本地*/ /*定位*/ File f = new File("D:/lab_2/用户名和密码.qq&quo ...

  4. Codeforces 713C Sonya and Problem Wihtout a Legend

    题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...

  5. poj1319Pipe Fitters

    链接 算不上几何的水题 第一种为(int)a*(int)b: 第二种分宽高交换两种讨论. 每一个的高度除第一个为1外其它的都可以看着b/sqrt(3.0)/2; #include <iostre ...

  6. openSUSE 国内镜像和镜像使用帮助 (zhuan)

    https://my.oschina.net/u/2475751/blog/631036?p={{currentPage-1}} https://lug.ustc.edu.cn/wiki/mirror ...

  7. RMAN-03009 ORA-19504 ORA-27038

    错误信息如下: RMAN> backup database tag='full20160112' format '/orabak/rman/full20160112' include curre ...

  8. Statspack安装配置及使用

    1.1 概念 statspack,用于收集系统信息,诊断数据库故障,也方便第三方技术支持进行远程阅读和建议.它连续收集数据信息,能够提供趋势分析,同时也需要单独分配一个表空间来存储这些统计数据.即在安 ...

  9. IIS_Mvc发布

    网站发布步骤: 这部分是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接 ...

  10. wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问。

    wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问. 因为做项目,多人分工,需要局域网内访问各自的项目. 然后安装了wampserver2.5,Ap ...