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的更多相关文章

  1. [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 ...

  2. leetcode 318. Maximum Product of Word Lengths

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

  3. 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 ...

  4. 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 ...

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

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

  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. 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 ...

  8. [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 ...

  9. 【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 ...

  10. LeetCode 318. Maximum Product of Word Lengths (状态压缩)

    题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...

随机推荐

  1. phpweb成品网站最新版(注入、上传、写shell)

    注入:之所以鸡肋就是该漏洞利用安装文件 重新生成 配置文件 写入可执行代码 鸡肋1: 具有破坏性 动作非常大 重新写了配置文件 数据库连接文件鸡肋2: 有一定安全常识的站长都会删掉 install 目 ...

  2. Asp.net WebAPI 单元测试

    现在Asp.net webapi 运用的越来越多,其单元而是也越来越重要.一般软件开发都是多层结构,上层调用下层的接口,而各层的实现人员不同,一般大家都只写自己对应单元测试.对下层的依赖我们通过IOC ...

  3. Apache Storm 的历史及经验教训——Nathan Marz【翻译】

    英文原文地址 中英文对照地址 History of Apache Storm and lessons learned --项目创建者 Nathan Marz Apache Storm 最近成为了ASF ...

  4. mysql in 排序

    SELECT * FROM my_table WHERE id IN (30, 20, 80, 40) ORDER BY FIELD(id, 30, 20, 80, 40);

  5. python报错:SyntaxError: Non-ASCII character '\xe5'的解决方法

    最近在学习机器学习,上面的代码都是一些python代码,对于python只是会一些基础性的东西,刚才就遇到了一个比较low的问题,但是还是记录一下吧. 在python代码中出现了中文,但是我又把# - ...

  6. 用issnode+IIS来托管NodeJs Server

    用issnode+IIS来托管NodeJs Server之一:安装篇 用issnode+IIS来托管NodeJs Server之二:移植 用issnode+IIS来托管NodeJs Server之三: ...

  7. js中“==”与"==="的区别

    首先,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同的一定不等. 一言以蔽之:==先转换类型再 ...

  8. Raw-OS源代码分析之消息系统-Queue_Size

    分析的内核版本号截止到2014-04-15.基于1.05正式版.blogs会及时跟进最新版本号的内核开发进度,若源代码凝视出现"???"字样,则是未深究理解部分. Raw-OS官方 ...

  9. 11G RAC 中 OCR 及Voting Disk 相关操作

    一.启动oracle clusterware先决条件:Oracle High Availability Services daemon(OHASD)运行在所有集群节点上1.启动整个Oracle Clu ...

  10. GCC 编译使用动态链接库 LD

    可以把当前路径加入 /etc/ld.so.conf中然后运行ldconfig,或者以当前路径为参数运行ldconfig 2.把当前路径加入环境变量LD_LIBRARY_PATH中3. 如果你觉得不会引 ...