Maximum Product of Word Lengths
2016-03-20 17:24
351 查看
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.
位操作
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.
位操作
int maxProduct(vector<string>& words) { vector<int> mask(words.size()); int result = 0; for (int i=0; i<words.size(); ++i) { for (char c : words[i]) mask[i] |= 1 << (c - 'a'); for (int j=0; j<i; ++j) if (!(mask[i] & mask[j])) result = max(result, int(words[i].size() * words[j].size())); } return result; }
相关文章推荐
- 在软件开发的早期阶段为什么要进行可行性研究?应该从哪些方面研究目标系统的可行性?
- vs2013安装过程及使用心得
- 20145330《Java程序设计》第三周学习总结
- (6)框架技术解说
- 翻转一个数的二进制后的值
- C++作业2
- 如何实现一个教师与学生教学辅助平台?
- 浅谈C++类的继承与派生的关系
- 系统调用
- 简单的jQuery图片标注效果
- 玩骰子
- 样本间的距离或者相似度度量方法
- 03-百思不得姐(第三天)
- 数据库事务
- 三种循环语句
- Spring3.1.2与Hibernate4.1.8整合
- JVM内存区域划分Eden Space、Survivor Space、Tenured Gen,Perm Gen解释
- 实践中学习AngularJS中的表单
- 学习进度表第三周
- python学习学习