318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示
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的整数表示的更多相关文章
- leetcode 318. Maximum Product of Word Lengths
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...
- 318 Maximum Product of Word Lengths 最大单词长度乘积
给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
随机推荐
- 加速chrome之Vimium快捷键
使用Vimium一段时间,不能完全学习所有的快捷键.但是对这种简约,vim风格的设计还是非常敬佩. 下面是一些总结: Vimium快捷键 WIKI: 是一个开源的google chrome的扩展插件, ...
- WPF GroupBox样式
来源:http://code.msdn.microsoft.com/WPF-GroupBox-Style-1d9df7c5/ 效果: XAML CODE: <Window xmlns=" ...
- PL/SQL 听课笔记
PL/SQL: 知识回顾: SQL: 结构化查询语言: T-SQL: microsoft sql语言: PL/SQL: Oracle sql语言: 变量命名规则: 1.首字母必须是字母,可以包含字 ...
- jQuery.validate.js+API_cn
名称 返回类型 描述 validate(options) 返回:Validator 验证所选的FORM valid() 返回:Boolean 检查是否验证通过 rules() 返回:Options ...
- 使用myeclipse建立maven项目(重要)
maven是管理项目的,myeclipse是编写代码的.第一次写项目都要配置好多东西,很麻烦,now 来看看怎样新建一个maven项目. 工具/原料 myeclipse maven 方法/步骤 ...
- JavaWeb学习总结(二)—http协议
http协议概念: * 即超文本传输协议.它规定了浏览器与服务器之间的通讯规则. * http是基于请求/响应模式的,所以分为请求协议和响应协议 http的类型: HTTP协议的版本:HTTP/1.0 ...
- JavaSE复习_3 继承
△先默认初始化,在显示初始化,在构造函数初始化 △继承的弊端:代码的耦合性增加了. △子类不能继承父类的构造方法. △子类会拥有父类的私有成员变量,但是必须通过get,set方法访问. △super不 ...
- (二)miller指导查看主控板寄存器操作
Welcome to Command Shell!Username:admin Password:***** ROS>en ROS# ROS# ROS# ROS# ROS#^ada ROS(ad ...
- Request 接收参数乱码原理解析
起因: 今天早上被同事问了一个问题:说接收到的参数是乱码,让我帮着解决一下. 实际情景: 同事负责的平台是Ext.js框架搭建的,web.config配置文件里配置了全局为“GB2312”编码: &l ...
- easyui combobox 智能提示搜索
<!-- 获取机会点名称列表 --><script> function initOpportunityNameFuzzyQuery() { $('#jihuidianmingc ...