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:

Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
Return 16
The two words can be "abcw", "xtfn".

Example 2:

Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
Return 4
The two words can be "ab", "cd".

Example 3:

Given ["a", "aa", "aaa", "aaaa"]
Return 0
No such pair of words.

分析:

怎么判断两个string没有共同的letter。 我们可以用int的每一个位来表示是否某一个letter出现在某一个字符串中。如果两个字符串的int值AND以后为0,很明显,它们是没有公共letter.

 public class Solution {
public int maxProduct(String[] words) {
if (words == null || words.length <= ) return ; int[] wordInfo = new int[words.length];
for (int i = ; i < words.length; i++) {
for (int j = ; j < words[i].length(); j++) {
// each letter is the 1 bit in the number
wordInfo[i] = wordInfo[i] | ( << words[i].charAt(j) - 'a');
}
}
int maxProduct = ;
for (int i = ; i < words.length; i++) {
for (int j = i + ; j < words.length; j++) {
if (((wordInfo[i] & wordInfo[j]) == )) {
maxProduct = Math.max(maxProduct, words[i].length() * words[j].length());
}
}
} return maxProduct;
}
}

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

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

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

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

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

  8. [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 ...

  9. [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 ...

随机推荐

  1. emberJS

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. bootstrap fileinput添加上传成功回调事件

    国外牛人做的bootstrap fileinput挺酷的,但是可惜没有提供自定义上传成功回调事件的接口,因此感到非常头疼,但是很幸运的是,我在网上搜索到一个提问帖子,它问到使用Jquery的on函数绑 ...

  3. 后台管理UI推荐

    目录 一.EasyUI 二.DWZ JUI 三.HUI 四.BUI 五.Ace Admin 六.Metronic 七.H+ UI 八.其它UI 九.总结 最近要做一个企业的OA系统,以前一直使用Eas ...

  4. 【poj3020】 Antenna Placement

    http://poj.org/problem?id=3020 (题目链接) 题意 给出一个矩阵,矩阵中只有‘*’和‘o’两种字符,每个‘*’可以向它上下左右四个方位上同为‘*’的点连一条边,求最少需要 ...

  5. linux (centos) 单机50w+链接 内核参数配置

    1 突破系统最大fd   查看当前文件描述符的限制数目的命令: ulimit -n .修改文件描述符的限制数目 2.1 临时改变当前会话: ulimit -n 2.2 永久变更需要下面两个步骤: ./ ...

  6. pthread 学习系列 case2-- pthread_mutex_t

    许多互斥对象 如果放置了过多的互斥对象,代码就没有什么并发性可言,运行起来也比单线程解决方案慢.如果放置了过少的互斥对象,代码将出现奇怪和令人尴尬的错误.幸运的是,有一个中间立场.首先,互斥对象是用于 ...

  7. sqlmap注入检测

    1.列出可利用数据库: sqlmap  -u  url  --dbs 2.列出某个数据库中表: sqlmap  -u  url   --tables  -D  south sqlmap  -u  ur ...

  8. 行为Behavior的使用

    原文地址:http://www.it610.com/article/4918541.htm 行为就是继承yii\base\behavior,可以绑定到任意yii\base\compent实例上,然后这 ...

  9. 开源项目剖析之apache-common-pool

    前沿 该工程提供了对象池解决方案,该方案主要用于提高像文件句柄,数据库连接,socket通信这类大对象的调用效率.简单的说就是一种对象一次创建多次使用的技术. 整体结构 整个项目有三个包分别是org. ...

  10. 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1

    注:这里inc方法和dec方法加synchronized关键字是因为当两个线程同时操作同一个变量时,就算是简单的j++操作时,在系统底层也是通过多条机器语句来实现,所以在执行j++过程也是要耗费时间, ...