[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 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:16The two words can be
Explanation:"abcw", "xtfn".
Example 2:
Input:["a","ab","abc","d","cd","bcd","abcd"]
Output:4The two words can be
Explanation:"ab", "cd".
Example 3:
Input:["a","aa","aaa","aaaa"]
Output:0No such pair of words.
Explanation:
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 【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])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
随机推荐
- 2020/1/29 PHP代码审计之进一步学习XSS【持续更新】
0x00 上午学习了XSS漏洞,中午吃饭想了想,还是思考的太浅层了,这种老生常谈的东西对于现在的我意义不大.现在我需要的是思考.于是就有了这个随笔.在本文中,我会持续更新一些XSS的深入思考,payl ...
- Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之数字类型(number)
Python 数字类型(number)用于存储数值.数据类型是不允许改变的,这就意味着如果改变number数据类型的值,将重新分配内存空间. 1. 一个简单的示例: # 以下实例在变量赋值时数字类 ...
- ThreeJS 阴影条纹BUG
ThreeJS 开启阴影正确做法: 1. 渲染器启用阴影 renderer.shadowMap.enabled = true;2. 灯光产生阴影 light.castShadow = true;3. ...
- 吴裕雄--天生自然TensorFlow2教程:创建Tensor
import numpy as np import tensorflow as tf tf.convert_to_tensor(np.ones([2, 3])) tf.convert_to_tenso ...
- 吴裕雄--天生自然 JAVA开发学习: 循环结构
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System ...
- [代码审计]PCWAP
为什么想要审计这套源码呐?之前看到某大佬在做反钓鱼网站的时候,发现钓鱼网站的后台用的就是PCWAP,所以我觉得有必要审计一下,顺便记录,打击网络犯罪! 0x00 PCAWAP: PCWAP手机网站建站 ...
- PHP实现冒泡排序算法相关案例
<?php /** * 冒泡排序,先找出一个最大的浮上去,然后再依次找最大的浮上去 */ function mpsort($a = []){ $nCount = count($a); if($n ...
- Python map filter reduce enumerate zip 的用法
map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...
- Ackermann函数
Ackermann函数定义如下: 若m=0,返回n+1. 若m>0且n=0,返回Ackermann(m-1,1). 若m>0且n>0,返回Ackermann(m-1,Ackerman ...
- ZJNU 1535 - 新建的大楼--中高级
因为从俯视图看,输入输出的视角是从右下方看向左上方的 所以左上角的正方体最有可能被其他正方体挡住 立体上,底部的正方体最有可能被顶部的正方体挡住 所以绘图应该从后往前,从下往上绘制 剩下的就是一大堆计 ...