[LeetCode] 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.
这道题给我们了一个单词数组,让我们求两个没有相同字母的单词的长度之积的最大值。我开始想的方法是每两个单词先比较,如果没有相同字母,则计算其长度之积,然后每次更新结果就能找到最大值。但是我开始想的两个单词比较的方法是利用哈希表先将一个单词的所有出现的字母存入哈希表,然后检查另一个单词的各个字母是否在哈希表出现过,若都没出现过,则说明两个单词没有相同字母,则计算两个单词长度之积并更新结果。但是这种判断方法无法通过OJ的大数据集,上网搜大神们的解法,都是用了mask,因为题目中说都是小写字母,那么只有26位,一个整型数int有32位,我们可以用后26位来对应26个字母,若为1,说明该对应位置的字母出现过,那么每个单词的都可由一个int数字表示,两个单词没有共同字母的条件是这两个int数想与为0,用这个判断方法可以通过OJ,参见代码如下:
解法一:
class Solution {
public:
int maxProduct(vector<string>& words) {
int res = ;
vector<int> mask(words.size(), );
for (int i = ; i < words.size(); ++i) {
for (char c : words[i]) {
mask[i] |= << (c - 'a');
}
for (int j = ; j < i; ++j) {
if (!(mask[i] & mask[j])) {
res = max(res, int(words[i].size() * words[j].size()));
}
}
}
return res;
}
};
还有一种写法,借助哈希表,映射每个mask的值和其单词的长度,每算出一个单词的mask,遍历哈希表里的值,如果和其中的mask值相与为0,则将当前单词的长度和哈希表中存的单词长度相乘并更新结果,参见代码如下:
解法二:
class Solution {
public:
int maxProduct(vector<string>& words) {
int res = ;
unordered_map<int, int> m;
for (string word : words) {
int mask = ;
for (char c : word) {
mask |= << (c - 'a');
}
m[mask] = max(m[mask], int(word.size()));
for (auto a : m) {
if (!(mask & a.first)) {
res = max(res, (int)word.size() * a.second);
}
}
}
return res;
}
};
参考资料:
https://leetcode.com/discuss/74580/bit-shorter-c
https://leetcode.com/discuss/75034/clear-and-easily-understanding
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Maximum Product of Word Lengths 单词长度的最大积的更多相关文章
- [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
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- [Swift]LeetCode318. 最大单词长度乘积 | 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 最大单词长度乘积
给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
- Maximum Product of Word Lengths -- LeetCode
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 解题报告(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 ...
- 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 ...
- 【leetcode】Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
随机推荐
- ASP.NET Core 中文文档 第二章 指南(4.3)添加 View
原文:Adding a view 作者:Rick Anderson 翻译:魏美娟(初见) 校对:赵亮(悲梦).高嵩(Jack).娄宇(Lyrics).许登洋(Seay).姚阿勇(Dr.Yao) 本节将 ...
- 【中文分词】条件随机场CRF
之前介绍的MMEM存在着label bias问题,因此Lafferty et al. [1] 提出了CRF (Conditional Random Field). BTW:比较有意思的是,这篇文章的二 ...
- Golang汇编命令解读
我们可以很容易将一个golang程序转变成汇编语言. 比如我写了一个main.go: package main func g(p int) int { return p+1; } func main( ...
- python 数据类型---列表使用 之一
列表的表现形式:其中的元素可以使任何数据类型,像 字符串,数字, 字典, 列表,变量 等任何类型 age = 28 name = ["Frank", "Lee" ...
- [修正] Firemonkey 中英文混排折行问题(移动平台)
问题:FMX 在移动平台的文字显示并非由该平台的原生 API 来显示,而是由 FMX.TextLayout.GPU 来处理,也许是官方没留意到中文字符的问题,造成在中英文混排折行时,有些问题. 适用: ...
- 浮动清除、before&after
::before 和 ::after属于伪元素,而 :before 和 :after属于伪类(CSS3 中为了区别伪元素和伪类为伪元素使用了双冒号)因此如果使用了 display 或者 width 等 ...
- 分布式任务&分布式锁(li)
目前系统中存在批量审批.批量授权等各个操作,批量操作中可能因为处理机器.线程不同,造成刷新缓存丢失授权等信息,如批量审批同一用户权限多个权限申请后,流程平台并发的发送多个http请求到acl不同服务器 ...
- Linux:JDK配置
1.JDK官网下载"jdk-8u101-linux-i586.tar.gz",32位或64位. 2 命令 #创建jdk所在目录 sudo mkdir /usr/lib/jvm #找 ...
- ES6之解构赋值
截止到ES6,共有6种声明变量的方法,分别是var .function以及新增的let.const.import和class: 我们通常的赋值方法是: var foo='foo'; function ...
- ArcGIS Engine开发之空间查询
空间查询功能是通过用户选择的空间几何体以及该几何体与当前地图中要素之间的几何关系进行空间查找,从而得到查询结果的操作. 相关类与接口 空间查询相关的类主要是SpatialFilter类,其实现的接口主 ...