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.

    public static int maxProduct(String[] words){
int[] arri = new int[words.length];
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words[i].length(); j++) {
arri[i] |= 1<<(words[i].charAt(j)-'a');
}
} int ans = 0;
for (int i = 0; i < words.length-1; i++) {
for (int j = i+1; j < words.length; j++) {
if ((arri[i] & arri[j]) == 0) {// 没有重复的字母
ans = Math.max(ans, words[i].length()*words[j].length());
}
}
}
return ans;
}

移位运算符:

1 << 0 => 1
1 << 1 => 2
1 << 2 => 4
1 << 3 => 8
1 << 4 => 16
1 << 5 => 32
1 << 6 => 64
1 << 7 => 128
1 << 8 => 256
1 << 9 => 512
1 << 10 => 1024
1 << 11 => 2048
1 << 12 => 4096
1 << 13 => 8192
1 << 14 => 16384
1 << 15 => 32768
1 << 16 => 65536
1 << 17 => 131072
1 << 18 => 262144
1 << 19 => 524288
1 << 20 => 1048576
1 << 21 => 2097152
1 << 22 => 4194304
1 << 23 => 8388608
1 << 24 => 16777216
1 << 25 => 33554432
 
words[i] 对应着arri[i]:words[i]中的每个单词编码后得到arri[i]
注释处相交等于0说明有重复字母。
 
 

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

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

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

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

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

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

  8. 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...

  9. 318 Maximum Product of Word Lengths 最大单词长度乘积

    给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...

随机推荐

  1. codeforces 83 D. Numbers

    题意: 给出l,r,k,(1 ≤ l ≤ r ≤ 2·109, 2 ≤ k ≤ 2·109) 求在区间[l,r]内有多少个数i满足 k | i,且[2,k-1]的所有数都不可以被i整除 首先,如果k不 ...

  2. Ubuntu开启22端口

    [http://blog.csdn.net/baple/article/details/39288817] 安装OpenSSH Ubuntu缺省没有安装SSH Server,使用以下命令安装: sud ...

  3. nginx长连接设置

    http { keepalive_timeout 20; --长连接timeout keepalive_requests 8192; --每个连接最大请求数} events { worker_conn ...

  4. Software Development Principle

    Every great piece of software begins with customer's big idea. As a professional softeware developer ...

  5. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体旋转)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  6. Python文本处理——中文标点符号处理

    中文文本中可能出现的标点符号来源比较复杂,通过匹配等手段对他们处理的时候需要格外小心,防止遗漏.以下为在下处理中文标点的时候采用的两种方法,如有更好的工具,请推荐补充. 中文标点集合 比较常见标点有这 ...

  7. expr 命令

    求表达式变量的值.expr 命令读入 Expression 参数,计算它的值,然后将结果写入到标准输出. 用法:expr Expression Expression 参数规则: 用空格隔开每个项. 用 ...

  8. Momo自定义DialogFragment

    在Fragnment弹窗提示 XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  9. oracle安装不容易啊

    oracle 配置方面,电脑端使用的是ORALE 10G 64位,PLSQL DEVELOPER使用32位,ORACLE CLIENT使用11.5版本.之前一直因为ORACLE 使用32位,导致在电脑 ...

  10. 使用python+pychram进行API测试(接口测试)初级STEP 1

    花了一天时间安装了解了下最基本的python+pychram进行API测试,下面这个可以指导自己以后入门:基本的开发级别还需要学习 1.python下载地址:https://www.python.or ...