LeetCode-Maximum Product of Word Lengths
Description:
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
题目大意:找出不包含相同字母的单词长度乘积的最大值
思路:最容易想到的办法应该是3层循环枚举,找出最大值,复杂度是O(n^2*k),k为单词的长度。在此基础上优化,优化判断两个单词是否包含相同字母部分,可以使用26位的整数来表示单词的字母情况,每个字母在这个整数的二进制数中都有一个位置,如果为1代表包含这个字母,否则相反。再通过两个整数按位与来判断是否包含相同字母,如果不包含相同字母则两个整数的二进制数中必然没有对应位置都为1的位,则按位与后得0。这样优化之后复杂度变为O(n^2)。还可以再优化,可以预先把字符串数组按长度降序排序,这样在寻找最大乘积的时候就可以尽早的找到,不做过多的计算。但是这种优化不是一定能提高效率的,因为如果遇到最后的结果是比较小的情况,并不能避免大量的计算,还增加了排序的开销。
含有排序优化的实现代码:
public class Solution {
public int maxProduct(String[] words) {
int[] map = new int[words.length]; //长度由大到小排序
Arrays.sort(words, new Comparator<String>() {
public int compare(String s1, String s2) {
return s2.length() - s1.length();
}
}); //用26位整数记录包含字母的情况
for(int i=0; i<words.length; i++) {
int bit = 0;
for(int j=0; j<words[i].length(); j++) {
bit |= (1 << words[i].charAt(j) - 'a');
}
map[i] = bit;
} int max = 0;
//找出满足条件的最大乘积
for(int i=0; i<words.length; i++) {
if(words[i].length()*words[i].length() <= max) break; //往后没有更大的了
for(int j=i+1; j<words.length; j++) {
if((map[i] & map[j]) == 0) { //没有相同的字母
max = Math.max(max, words[i].length() * words[j].length()); //一次循环中最大的
break;
}
}
}
return max;
}
}
LeetCode-Maximum Product of Word Lengths的更多相关文章
- [LeetCode] 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
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- 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 ...
- Maximum Product of Word Lengths -- LeetCode
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 ...
- 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 ...
- 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单词长度最大乘积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 【leetcode】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 (状态压缩)
题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...
随机推荐
- MiniDao普通项目集成方案
1.导入必要的jar包: 2.spring配置文件增加如下配置: <!-- Hibernate工具栏配置--> <bean id="miniDaoHiberCommonDa ...
- Determining if a point lies on the interior of a polygon
Determining if a point lies on the interior of a polygon Written by Paul Bourke November 1987 Solut ...
- Redis之高可用方案
Redis之高可用方案 Redis以其高效的访问速度著称.但由于官方还未发布redis-cluster,而redis的replica又有诸多不便:比如一组master-slave的机器,如果之间有 ...
- How to import library ?
Android Studio: Download or git the library. (for example: the library folder named ActionBarSherloc ...
- TP收集一些可以用的资源
http://www.thinkphp.cn/code/2184.html UI http://www.thinkphp.cn/code/2158.html 报名 http://www.th ...
- 像table一样布局div
原文:http://www.456bereastreet.com/archive/200405/equal_height_boxes_with_css/ 下面是我翻译的内容,是根据我对文章的理解意译的 ...
- IIS出现The specified module could not be found解决方法
打开IIS 信息服务,在左侧找到自己的计算机,点右键,选择属性,在主属性中选编辑,打开“目录安全性”选项卡,单击“匿名访问和验证控制”里的“编辑”按钮,在弹出的对话框中确保只选中了“匿名访问”和“集成 ...
- Backbone1.0.0数据验证的变化
0.5.3版本对Model数据验证时,绑定Error就可以了: (function(){ var Model = Backbone.Model.extend({ initialize : functi ...
- Application MyTest has not been registered. This is either due to a require() error during initialization or failure to call AppRegistry.registerComponent.
运行react-native项目时报错. 说明一下:项目本来是好的,再次运行就报错了 解决解决办法倒是有,不过具体什么原因不知道.希望有知道具体原因的童鞋能够补充一下 第一种情况:真的是注册的时候写错 ...
- 终于找到全annotation配置springMVC的方法了(事务不失效)
如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行 这个问题是由于问答上有解决方案 引用 这个问题很经典了 在主容器中 ...