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;
}
}

最新文章

  1. C# HttpHelper 采集
  2. iOS-分段控制器-基本概念
  3. LINUX重启MYSQL的命令
  4. Mysql or Mongodb LBS快速实现方案
  5. InstallShield: Component-Feature Associations
  6. Wix 安装部署(一)同MSBuild 自动生成打包文件 转
  7. CF402E Strictly Positive Matrix 传递闭包用强连通分量判断
  8. c语言学习
  9. $(obj).data() 绑定和获取数据的应用
  10. 【编译原理】语法分析LL(1)分析法的FIRST和FOLLOW集
  11. 04737_C++程序设计_第10章_面向对象设计实例
  12. 把vim变成C编辑器
  13. python 第三方库下载
  14. Django之路:模型(数据库)和自定义Field以及数据表的更改
  15. 201521123055 《Java程序设计》第4周学习总结
  16. WebBrowser加载一个URL被多次调用DocumentCompleted 的问题解决方案&lt;转&gt;
  17. BZOJ3737 : [Pa2013]Euler
  18. 内存管理-slab[代码]
  19. 设计模式之Strategy(策略)(转)
  20. wxPython的使用--类似画板的界面

热门文章

  1. 网鼎杯-Fakebook-反序列化和SSRF和file协议读取文件
  2. js根据当前日期 求一个月前 半年前 一年前的日期
  3. D10 基本数据类型(各种职业的技能分析) 主要为 int 和 str
  4. 收藏基本Java项目开发的书
  5. RedHat无法使用yum源问题
  6. 题解 LOJ-6485 【LJJ学二项式定理】
  7. 14)载入png图片
  8. Cocoapod-终端
  9. 实践一次有趣的sql优化
  10. Spring AOP中使用args表达式访问目标方法的参数