318. Maximum Product of Word Lengths
来源:互联网 发布:购物系统源码 编辑:程序博客网 时间:2024/06/04 19:14
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 class Solution { public int maxProduct(String[] words) { int res = 0;if (words.length <= 1)return 0;int[] numbers = new int[words.length];// 把字符串用二进制保存for (int i = 0; i < words.length; i++) {for (int j = 0; j < words[i].length(); j++) {numbers[i] |= 1 << (words[i].charAt(j) - 'a');}}for (int i = 0; i < words.length - 1; i++) {for (int j = i + 1; j < words.length; j++) {if ((numbers[i] & numbers[j]) == 0) {res = Math.max(res, words[i].length() * words[j].length());}}}return res; }}
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
- 给MFC对话框加背景图
- 剑指offer面试题 java解答66-67
- C# :this的用法(冒号后面的this)
- LRU算法实现
- [Html&JS] 一个网页上,不同的子父窗口iframe之间如何相互调用
- 318. Maximum Product of Word Lengths
- mysql查询今天,昨天,近7天,近30天,本月,上一月数据方法
- 技术债务:究竟让你付出了多大代价?
- springAOP事物管理不生效原因总结
- C/C++面试题(2)——查找链表中倒数第k个数
- 倒计时
- java设计模式之工厂方法模式
- activemq中多个consumer引起接收不到信息
- 关于用JAX-WS调用webservice异常:Server did not recognize the value of HTTP Header SOAPAction: .