[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])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
随机推荐
- php 设计模式之策略者模式
<?php header('Content-Type:text/html;charset=utf-8'); /** * 策略模式演示代码 * * 为了更好地突出“策略”,我们这里以出行为例演示, ...
- 清除input表单内容
碰到几次情况,页面刷新或者从上级页面返回表单的内容依然遗留,很影响使用. <form action="" method="" autocomplete=& ...
- BP算法推导python实现
def sigmoid(inX): return 1.0/(1+exp(-inX)) '''标准bp算法每次更新都只针对单个样例,参数更新得很频繁sdataSet 训练数据集labels 训练 ...
- vue执行期间的函数
先放上vue官方给的函数图
- java的io字符流关闭和刷新.flush();
因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中. 但是关闭的流对象,是无法继续写出数据 的.如果我们既想写出数据,又想继续使用流,就需要 flush 方法了. flush :刷新缓冲区, ...
- 关于mysql一边查一边更新
update test_table set user_id = 112 where id in (select id from ( select id from test_table where nu ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集基本使用方法
import tempfile import tensorflow as tf # 1. 从数组创建数据集. input_data = [1, 2, 3, 5, 8] dataset = tf.dat ...
- 吴裕雄--天生自然 PYTHON3开发学习:OS 文件/目录方法
import os, sys # 假定 /tmp/foo.txt 文件存在,并有读写权限 ret = os.access("/tmp/foo.txt", os.F_OK) prin ...
- 怎么通过scanf读取一个空白前的字符
/************************************************************************* > File Name: scanf2.c ...
- Matlab高级教程_第二篇:Matlab相见恨晚的模块_02_并行运算-利用GPU并行执行MATLAB程序
1 MATLAB原文: 如果所有你想使用的函数支持GPU,你能够使用gpuArray把输入的数据传输到GPU,也能够唤起gather命令把传输值GPU的数据回收. 2 通过gpuDevice命令观察当 ...