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

Example 3:

Input: ["a","aa","aaa","aaaa"]
Output: 0
Explanation:
No such pair of words.
class Solution {
public int maxProduct(String[] words) {
int[] checker = new int[words.length];
if (words == null || words.length == 0) {
return 0;
}
int res = 0; for (int i = 0; i < words.length; i++) {
String word = words[i];
for (int j = 0; j < word.length(); j++) {
char curChar = word.charAt(j);
checker[i] |= 1 << curChar - 'a';
}
} for (int i = 0; i < words.length - 1; i++) {
for (int j = i + 1; j < words.length; j++) {
if ((checker[i] & checker[j]) == 0) {
res = Math.max(res, words[i].length() * words[j].length());
}
}
}
return res;
}
}
public class Solution {
public int largestProduct(String[] dict) {
// Write your solution here
Arrays.sort(dict, new Comparator<String>(){
@Override
public int compare(String a, String b) {
return b.length() - a.length();
}
});
int[] arr = new int[dict.length];
for (int i = 0; i < dict.length; i++) {
for (int j = 0; j < dict[i].length(); j++) {
arr[i] |= 1 << dict[i].charAt(j) - 'a';
}
}
int res = 0;
for (int i = 1; i < dict.length; i++) {
for (int j = 0; j < i; j++) {
if (dict[i].length() * dict[j].length() <= res) {
break;
}
if ((arr[i] & arr[j]) == 0) {
res = dict[i].length() * dict[j].length();
}
}
}
return res;
}
}

[LC] 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 【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. 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 tw ...

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

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

  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. 【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. String,StringBuffer与StringBuilder的区别与选择

    三者的区别 String:不可变类,一旦一个对象被建立的时候,包含在这个对象中的字符串序列是不可变的,直到这个对象被销毁.StringBuffer:可变字符序列的字符串.当其对象被创建的时候,可以用a ...

  2. 洛谷 P1060开心的金明

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”.今 ...

  3. C语言程序设计|05

    问题 回答 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/9772 我在 ...

  4. Fib数列问题(项数很大)

    用fib(n)表示斐波那契数列的第n项,现在要求你求fib(n) mod m.fib(1)= 1, fib(2)= 1. 输入格式 输入2个整数n(1≤n≤1018), m(2≤m≤10000000) ...

  5. Centos7.6部署rsyslog+loganalyzer+mysql日志管理服务器

    参考来自: the_script :https://yq.aliyun.com/articles/675198 名山.深处:https://www.cnblogs.com/skychenjiajun/ ...

  6. Java使用Sftp实现对跨服务器上传、下载、打包、写入相关操作

    1.Maven引入jar <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch< ...

  7. 2.Jenkins结合k8s完成Jenkins slave功能

    1.构建镜像 下载基础镜像,这里使用openvz的包,下载地址为:https://wiki.openvz.org/Download/template/precreated,下载centos7的镜像 下 ...

  8. deque & list

    deque 双向队列 它也是采用动态数组的方式来管理的提供了随机数组 和vector的区别 1.deque头尾两端可以开放,能够进行快速的插入和删除(vector只能在尾部进行快速的插入和删除) 2. ...

  9. Maven--传递性依赖和依赖范围

    依赖范围不仅可以控制依赖与三种 classpath 的关系,还对传递性依赖产生影响. 假设 A 依赖于 B,B依赖于 C,我们说 A 对于 B 是第一直接依赖,B 对于 C 是第二直接依赖,A 对于 ...

  10. 使用tcpdump查看HTTP请求响应 详细信息 数据

    安装tcpdump: sudo yum install tcpdump 查看get请求: tcpdump -s 0 -A 'tcp dst port 80 and tcp[((tcp[12:1] &a ...