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:

Input: ["abcw","baz","foo","bar","xtfn","abcdef"]
Output: 16
Explanation: The two words can be "abcw", "xtfn".

Example 2:

Input: ["a","ab","abc","d","cd","bcd","abcd"]
Output: 4
Explanation: The two words can be "ab", "cd".

题意:

给定一堆单词,要求找出俩单词长度的最大乘积,要求俩单词不能有相同字母。

思路:

判断noOverLapping: 用2个set分别标记俩单词, 扫一遍set,若发现某个字母被同时标记过,则有重叠。

取最大乘积长度:两重for循环,两两比较,更新最大值。

代码:

 class Solution {
public int maxProduct(String[] words) {
int result = 0;
for (int i = 0; i < words.length; ++i) {
for (int j = i + 1; j < words.length; ++j) {
int tmp = words[i].length() * words[j].length();
if ( noOverLapping(words[i], words[j])&& tmp > result) {
result = tmp;
}
}
}
return result;
}
private boolean noOverLapping(String a , String b){
boolean[] setA = new boolean[256];
boolean[] setB = new boolean[256]; for(int i = 0; i < a.length(); i++){
setA[a.charAt(i)] = true;
} for(int i = 0; i < b.length(); i++){
setB[b.charAt(i)] = true;
} for(int i = 0; i < 256; i++){
if(setA[i] == true && setB[i] == true){
return false;
}
} return true;
}
}

可以进一步优化:对于如何判断俩单词有没有相同字母,可用位向量表示每个字母是否出现即可,俩位向量异或即可得出是否有相同字母。

 public class Solution {
public int maxProduct(String[] words) {
final int n = words.length;
final int[] hashset = new int[n]; for (int i = 0; i < n; ++i) {
for (int j = 0; j < words[i].length(); ++j) {
hashset[i] |= 1 << (words[i].charAt(j) - 'a');
}
} int result = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int tmp = words[i].length() * words[j].length();
if ((hashset[i] & hashset[j]) == 0 && tmp > result) {
result = tmp;
}
}
}
return result;
}
}

[leetcode]318. Maximum Product of Word Lengths单词长度最大乘积的更多相关文章

  1. leetcode 318. Maximum Product of Word Lengths

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

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

  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. 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 (状态压缩)

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

  6. Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算

    先介绍下本题的题意: 在一个字符串组成的数组words中,找出max{Length(words[i]) * Length(words[j]) },其中words[i]和words[j]中没有相同的字母 ...

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

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

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

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

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

随机推荐

  1. 【Linux_Unix系统编程】Chapter10 时间

    chapter10 时间 1:真实时间:度量这一时间的起点有二:(1)某个标准点:(2)进程生命周期内的某个固定时点(通常为程序启动) 2:进程时间:一个进程所使用的CPU时间总量,适用于对程序,算法 ...

  2. tomcat启动原理

    2018年04月12日 19:55:22 太极小帅帅 阅读数:282   前言 一直在用Tomcat,但是对其启动原理一直没去研究,这里准备去面试,可能会问道.于是总结了下启动原理.完全凭感觉去揣测, ...

  3. python+selenium+requests爬取我的博客粉丝的名称

    爬取目标 1.本次代码是在python2上运行通过的,python3的最需改2行代码,用到其它python模块 selenium 2.53.6 +firefox 44 BeautifulSoup re ...

  4. python入门-操作列表

    1 Python根据缩进来进行判断代码行与前一个代码行的关系 for name in names: print(name) names = ['baker','david','philp','rose ...

  5. Windows系统日常运维

    WINDOWS系统日常运维 http://www.docin.com/p-677263438.html

  6. linux更新系统

    1.linux更新系统 https://www.jb51.net/os/RedHat/450223.html 仅更新系统,不更新内核 yum -y --exclude=kernel\* upgrade ...

  7. 21. oracle游标循环例子

    事例1: create or replace procedure sp_addProjectQj( ret out number, flowid in number --流程Id) ascursor ...

  8. ABAP-增强-层级BOM-AB件业务

    目前新需求:整车A下挂有委外总成件B,总成件B和子件E是层级BOM,且采购类型均为F,信息记录类型均为寄售,按照现在标准MRP逻辑,只能计算第一层级子件需求,无法运行出子件E的需求. 1.实现方式 1 ...

  9. Oracle SQL Developer在进行查询的时候只显示50条数据

    在查询结果大于50条的时候,软件默认会只显示50条,向下拉会继续显示. 想要显示所有结果的话,光标放在结果集:ctrl+End或者是ctrl+PgDn都可以.

  10. jenkins 没有maven选项,怎么办

    第一步: 进入jenkins,点系统管理 第二:插件管理 点击“可选插件”  然后在右边的过滤输入框中输入搜索关键字: Maven Integration  或者 Pipeline Maven Int ...