318. Maximum Product of Word Lengths
来源:互联网 发布:webservice 返回json 编辑:程序博客网 时间:2024/06/14 18:55
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.
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
public class Solution { public int maxProduct(String[] words) { int[] binary = new int[words.length]; for (int i = 0; i < binary.length; i++) { for (char ch: words[i].toCharArray()) { binary[i] |= (1 << (ch - 'a')); } } int max = 0; for (int i = 0; i < binary.length; i++) { for (int j = i + 1; j < binary.length; j++) { if ((binary[i] & binary[j]) == 0) { max = Math.max(max, words[i].length() * words[j].length()); } } } return max; }}
阅读全文
0 0
- 【leetcode】318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths LeetCode
- [leetcode] 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- LeetCode: 318. Maximum Product of Word Lengths
- LeetCode 318. Maximum Product of Word Lengths
- leetcode 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- [LeetCode]318. Maximum Product of Word Lengths
- [LeetCode]318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- LeetCode 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- LeetCode *** 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- Java协变返回类型
- 安卓几种图片加载框架的比较
- Hadoop伪分布式集群部署
- Java 编程题目 第二十一题
- (141)间接光照缓存
- 318. Maximum Product of Word Lengths
- ubuntu下boot空间不足解决方法(卸载旧内核)
- bzoj1924 [Sdoi2010]所驼门王的宝藏
- 调试显示线程名称
- 字符串的输入
- 重建二叉树
- windows下mysql修改密码
- JDBC了解(1)
- (142)光照函数