[leetcode] 318. Maximum Product of Word Lengths
来源:互联网 发布:seo前沿者 编辑:程序博客网 时间:2024/05/16 00:46
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.
解法一:
一开始以为有linear的解法,没有想出来。后来O(n^2)的解法用hash table还是不行。
现在这个思路是把每一个单词转成一个int(32位),出现该字母就将对应的bit置一。如果两个单词具有相同的字母,那么他们的&操作不为0。
class Solution {public: int maxProduct(vector<string>& words) { int res = 0; int num = words.size(); vector<int> m(num,0); for(int i=0; i<num;++i){ for(char a:words[i]) m[i] |= 1<<(a-'a'); for(int j=0;j<i;++j){ if((m[i]&m[j])==0){ res = max(res,int(words[i].size()*words[j].size())); } } } 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
- 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]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
- 根据 url地址 下载图片
- Android Studio+ideasmali动态调试smali汇编
- git stash
- 第一课:scala基础入门实战
- POJ 2485 Highways
- [leetcode] 318. Maximum Product of Word Lengths
- 阿里大鱼发短信SDK遇到的一点问题
- ffmpeg常用基本命令
- gsoap 天气预报
- 获取视频的缩略图
- cron表达式详解
- 开发必知必用网站(持续更新)
- redis常用命令
- Insert Delete GetRandom O(1)