Leetcode 318.最大单词长度乘积
最大单词长度乘积
给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
示例 1:
输入: ["abcw","baz","foo","bar","xtfn","abcdef"]
输出: 16
解释: 这两个单词为"abcw", "xtfn"。
示例 2:
输入: ["a","ab","abc","d","cd","bcd","abcd"]
输出: 4
解释: 这两个单词为 "ab", "cd"。
示例 3:
输入: ["a","aa","aaa","aaaa"]
输出: 0
解释: 不存在这样的两个单词。
使用比特进行哈希。
public class Solution {
public int maxProduct(String[] words) {
int[] f = new int[words.length];
for(int i=0; i<words.length; i++) {
char[] wa = words[i].toCharArray();
for(int j=0; j<wa.length; j++) {
f[i] |= 1 << (wa[j]-'a');
}
}
int max = 0;
for(int i=0; i<words.length-1; i++) {
for(int j=i+1; j<words.length; j++) {
if ((f[i] & f[j]) == 0) max = Math.max(max, words[i].length() * words[j].length());
}
}
return max;
}
}
Leetcode 318.最大单词长度乘积的更多相关文章
- Java实现 LeetCode 318 最大单词长度乘积
318. 最大单词长度乘积 给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母.你可以认为每个单词只包含小 ...
- [Leetcode] 第318题 最大单词长度乘积
一.题目描述 给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母.你可以认为每个单词只包含小写字母.如果不 ...
- 318 Maximum Product of Word Lengths 最大单词长度乘积
给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
- [Swift]LeetCode318. 最大单词长度乘积 | 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单词长度最大乘积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- [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 ...
- Leetcode 1160: 拼写单词
给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌 ...
- python set() leetcode 签到820. 单词的压缩编码
题目 给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A. 例如,如果这个列表是 ["time", "me", "bell& ...
- 程序设计入门——C语言 第8周编程练习 1 单词长度(4分)
第8周编程练习 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系统 ...
随机推荐
- linux的目录结构详细介绍
linux的目录结构详细介绍 1. /目录(根目录) 2./ect/目录 特定主机系统范围内的配置文件. 3./usr/目录 默认软件都会存于该目录下.用于存储只读用户数据的第二层次:包含绝大多数的用 ...
- Eclipse的ant调用maven
需要在 eclipse 的 windows - preferences - ant - runtime - classpath - global entries 加入 eclipse 里面的 jsch ...
- Apex 使用和学习
ref doc http://o7planning.org/en/10345/oracle-apex-tutorial-for-beginners (change from web to pdf) ...
- 485 Max Consecutive Ones 最大连续1的个数
给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意: 输入的数组只包 ...
- KMS算法
解题:http://hihocoder.com/problemset/problem/1015 KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt同时 ...
- leetcode315 Count of Smaller Numbers After Self
思路: bit + 离散化. 实现: #include <bits/stdc++.h> using namespace std; class Solution { public: int ...
- 【经验总结】北邮OJ
90. 字符串转换 时间限制 1000 ms 内存限制 65536 KB 题目描述 我们将仅由若干个同一小写字母构成的字符串称之为简单串,例如"aaaa"是一个简单串,而" ...
- Android Studio3.0 Error:Execution failed for task ':app:javaPreCompileDebug' 错误
Error:Execution failed for task ':app:javaPreCompileDebug'. > Annotation processors must be expli ...
- String.format()【示例详解】
String.format()[示例详解] 整理者:Vashon 前言: String.format 作为文本处理工具,为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 String.fo ...
- laravel学习笔记(二)
路由 HTTP方法:支持http1.1中所有类型传参方式,get,post,put,delete,options,patch Route::get($url,$callback); 路由参数: Rou ...