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 单词长度的最大积的更多相关文章

  1. [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 ...

  2. leetcode 318. Maximum Product of Word Lengths

    传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...

  3. [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 ...

  4. 318 Maximum Product of Word Lengths 最大单词长度乘积

    给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...

  5. 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 ...

  6. 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...

  7. 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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. oracle的特殊权限s bit丢失

    在SUN Cluster搭建过程中,主机工程师在配置集群资源组时报一个oracle的文件权限错误: 协助排查,发现报错很明确,直接指出说oracle的s bit 权限未设置. 直接去查看$ORACLE ...

  2. 基于STM32Cube的脉冲输出

    方法一:定时器定时I/O反转生成脉冲波形 1.建立STM32Cube选择STM32F429,我使用的STM32F429-discovery开发板,晶振是8MHz,时钟配置为180M,这样定时器内部时钟 ...

  3. ubuntu学习的简单笔记

    l vi编辑器开发步骤 A)输入 vi Hello.java B) 输入 i 插入模式. C)输入 冒号.[保存退出:wq][退出不保存:q!] l 列出当前目录的所有文件:ls 详细信息的列表:ls ...

  4. html文本垂直居中对齐

    html文本垂直居中对齐,代码如下: <div id="box" style="height:100px; line-height:100px; border:1p ...

  5. MySQL动态游标

    通过(准备语句+视图+静态游标)实现 -- 建立测试表和数据 create table webuser (username varchar(10)); insert into webuser valu ...

  6. GJM : Unity3D HIAR -【 快速入门 】 八、开发云识别应用

    开发云识别应用 为了解决识别图片数量限制,以及上线应用不能动态修改识别图片和 AR 内容的问题,我们在 HiAR SDK for Unity 新版本(v1.1.x 及后续版本)中集成了云识别功能.本文 ...

  7. Xcode7使用插件的简单方法&&以及怎样下载到更早版本的Xcode

    Xcode7自2015年9上架以来也有段时间了, 使用Xcode7以及Xcode7.1\Xcode7.2的小伙伴会发现像VVDocumenter-Xcode\KSImageNamed-Xcode\HO ...

  8. dSYM 文件分析工具

    来到新公司后,前段时间就一直在忙,前不久 项目 终于成功发布上线了,最近就在给项目做优化,并排除一些线上软件的 bug,因为项目中使用了友盟统计,所以在友盟给出的错误信息统计中能比较方便的找出客户端异 ...

  9. 读《乔布斯的NeXT和苹果之间,隔了这两个创业常识》

    原文链接:http://wwww.huxiu.com/article/114950/1.html 前言: 当今硅谷风头最劲的风险投资家马克·安德森曾说,硅谷每年大约诞生15-20家真正值得投资的公司. ...

  10. T-SQL 查询数据库中各个表的使用空间

    1. 查询单表 EXEC sp_spaceused 'dbo.tablename' 2. 查询所有表 SELECT t.NAME AS TableName, s.Name AS SchemaName, ...