Leetcode 318 Maximum Product of Word Lengths
来源:互联网 发布:淘宝代收货是什么意思 编辑:程序博客网 时间:2024/06/07 14:50
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.
先把每个string进行处理
value[i] |= 1<<(tmp.charAt(j) - 'a');
当两个string里面完全不重复的时候
两个value做&会等于0
public class Solution { public int maxProduct(String[] words) { if(words == null || words.length == 0){ return 0; } int[] value = new int[words.length]; for(int i = 0; i < words.length; i++){ String tmp = words[i]; for(int j = 0; j < tmp.length(); j++){ value[i] |= 1<<(tmp.charAt(j) - 'a'); } } int max = 0; for(int i = 0; i < words.length; i++){ for(int j = i + 1; j < words.length; j++){ if((value[i] & value[j]) == 0){ max = words[i].length() * words[j].length() > max ? words[i].length() * words[j].length() : max; } } } return max; }}
- [LeetCode 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
- leetcode 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
- LeetCode 318 Maximum Product of Word Lengths
- leetcode Maximum Product of Word Lengths
- Leetcode: Maximum Product of Word Lengths
- 【leetcode】318. Maximum Product of Word Lengths
- leetcode Maximum Product of Word Lengths
- leetcode -- Maximum Product of Word Lengths -- 重点
- Maximum Product of Word Lengths | LeetCode
- Leetcode: Maximum Product of Word Lengths
- LeetCode:Maximum Product of Word Lengths
- Leetcode: Maximum Product of Word Lengths
- 【剑指offer】面试题36:二叉搜索树与双向链表
- Java中Integer和int的区别
- hdu1006
- Java 设计模式_组合模式
- Python 使用技巧 常见问题
- Leetcode 318 Maximum Product of Word Lengths
- 设计模式-13-模板方法模式
- 计算机视觉小实例 No.1 基于直方图优化的图像去雾技术
- Windows下安装Nexus OSS 3.4.0
- 北美CS求学找工指南
- 建站的原型图是什么意思?
- 10款优秀的产品包装设计欣赏!
- QQ2018安全问题介绍
- 【Leetcode】【python】Binary Tree Inorder Traversal