leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)
https://leetcode.com/problems/maximum-product-of-word-lengths/
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.
class Solution {
public:
void strToBits(vector<int>& bits, vector<string>& words) {
for(int i=; i<words.size(); ++i) {
int tmp = ;
for(int j=; j<words[i].size(); ++j) {
int offset = words[i][j] - 'a';
tmp = (tmp | ( << offset));
}
bits[i] = tmp;
}
}
int maxProduct(vector<string>& words) {
int n = words.size();
if(n < ) return ;
vector<int> bits(n, );
strToBits(bits, words);
int res = ;
for(int i=; i<n; ++i) {
for(int j=i+; j<n; ++j) {
int len1 = words[i].length(), len2 = words[j].length();
if((bits[i] & bits[j]) == ) {
res = max(res, len1 * len2);
}
}
}
return res;
}
};
leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)的更多相关文章
- leetcode 318. Maximum Product of Word Lengths
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- Java [Leetcode 318]Maximum Product of Word Lengths
题目描述: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where ...
- [leetcode]318. Maximum Product of Word Lengths单词长度最大乘积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- LeetCode 318. Maximum Product of Word Lengths (状态压缩)
题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...
- Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算
先介绍下本题的题意: 在一个字符串组成的数组words中,找出max{Length(words[i]) * Length(words[j]) },其中words[i]和words[j]中没有相同的字母 ...
- 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...
- LeetCode 【318. Maximum Product of Word Lengths】
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 318. Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
随机推荐
- HDU4502吉哥系列故事——临时工计划
http://acm.hdu.edu.cn/showproblem.php?pid=4502 题意 :这个是中文题,我就不再详述了. 思路 : 以前做过一个活动区间选择,结果就按着那个思路敲了,后来发 ...
- DJANGO结合jQuery cxSelect 作二级菜单过滤
EN,到这个阶段,基本功能算是完成了. 使用了jQuery cxSelect这个插件. http://code.ciaoca.com/jquery/cxselect/ 相关代码如下: html: &l ...
- linux ubuntu卸载软件
1.通过deb包安装的情况: 安装.deb包: 代码:sudo dpkg -i package_file.deb反安装.deb包: 代码:sudo dpkg -r package_name 2.通过a ...
- HeadFirst设计模式之组合模式
一. 1.The Composite Pattern allows us to build structures of objects in the form of trees that contai ...
- WPF之Behavior
本文主要是以实现拖动元素作为例子. 创建Behavior: 通常这个类会继承自Behavior<T>,其中T就是此Behavior服务的对象,在此处使用的是UIElement,也就是虽有的 ...
- Servlet入门案例
开发servlet有三种方法: 1.实现Servlet接口; public interface Servlet { void init(ServletConfig var1) throws Servl ...
- SGU 101
SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上. #include <iostream> #include <vector> #inclu ...
- Vim常用命令手册
这两年工作基本都是用vim,用习惯发现到哪都离不开这玩意. 退出编辑器 :w 将缓冲区写入文件,即保存修改:wq 保存修改并退出:x 保存修改并退出:q 退出,如果对缓冲区进行过修改,则会提示:q! ...
- 关于 mysqladmin
>mysqladmin 工具的使用格式: mysqladmin [option] command [command option] command ...... option 选项: -c nu ...
- JSON格式转换成XML格式
第一种方法: 需要使用命名空间System.Runtime.Serialization.Json 下面有JsonReaderWriterFactory XmlDictionaryReader read ...