318. Maximum Product of Word Lengths
2016-03-20 17:15
573 查看
Given a string array
Example 1:
Given
Return 16
The two words can be “abcw”, “xtfn”.
Example 2:
Given
Return 4
The two words can be “ab”, “cd”.
Example 3:
Given
Return 0
No such pair of words.
https://leetcode.com/discuss/74580/bit-shorter-c
判断两个字符串有没有共同的字符(都是小写字母)
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.
https://leetcode.com/discuss/74580/bit-shorter-c
判断两个字符串有没有共同的字符(都是小写字母)
class Solution { public: int maxProduct(vector<string>& words) { int result = 0; if(words.empty()) return result; unordered_map<int ,int> maps; for(auto word : words){ int mask = 0; for(auto c : word) mask |= 1<<(c-'a'); //这句是精华,用比特位指示出现某个字符 maps[mask] = max(maps[mask],(int) word.size()); } for(auto a : maps) for(auto b : maps){ if(!(a.first & b.first)) result = max(result, a.second*b.second); } return result; } };
相关文章推荐
- Java多线程系列--“JUC线程池”02之 线程池原理(一)
- 分析恶意驱动
- 数据结构极值及平均值
- 鲜为人知的Titanium之线程
- bzoj 2143: 飞飞侠
- [随笔]GNU linker script,ld script,GNU链接脚本
- 构建之法阅读笔记(3)
- 2016/3/20 数组定义 数组遍历 超全局数组 数组元素设置(in_array() 、array_reverse()、count()、array_unique()、unset()、array_values、array_merge、array_push) 列表实例
- occ 中绘制二维矩形
- 业务事件模型的实现
- iOS中消息的传递机制
- 在家乡做什么生意比较好?且看雷家第一智囊的分析
- Python Design Patterns
- 二进制,8进制,16进制,10进制
- 在家乡做什么生意比较好?且看雷家第一智囊的分析
- 第三周进度条
- JDBC
- 第三周学习总结
- 基础补完系列 - C++ Primer Plus 第二章
- 图灵测试,测的到底是什么?