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.

思路:朴素的方法是O(n^2),我们可以在这个基础上进行剪枝。

这个题目有两个技巧:一个是如何最快地判断两个字符串是否含有相同的字符,另一个是如何进行剪枝。

位运算判断相同字符:

对于给的每一个字符串,我们用一个int变量来存储它的字符分布。由于给的字符串只含有26个小写字母,我们只需要26位就能存储下来(若某个字母出现,就将对应的位置为1,否则为0)。当我们需要判断两个字符串是否含有相同的字符时,只需将对应的两个int变量进行与运算,若结果为0则不含有相同字符,否则就是有。

剪枝:

将给的所有字符串按照长度进行排序。

调用stl的sort函数时,我们需要重新定义比较函数。当比较函数在类内部定义时,要定义为静态函数。或者直接定义为全局函数。因此leetcode对静态函数支持不好,所以我用全局函数来实现。

我们先从最后两个字符串进行考虑,因为这两个的长度乘积是所有可能中最大的。

我们用两个指针来指向他们:假设i是最后一个字符串,j是倒数第二个。

我们将j依次向前移动,直到字符串i和j不含有重复字符。

记录下此时两个字符串长度的乘积,用res来表示。然后j不再需要进行前移,因为再往前j所指向的字符串的长度只会变小,不可能获得更大的结果。

此时,唯一可能产生更大结果的情况是:存在两个字符串m和n,它们的下标构成关系j < m < n < i。可以看到,这时m和n的字符串长度都不小于j,因此两者的乘积有可能会大于当前的结果。这里我们用floor来表示j所在的位置。

因此我们将i前移一位,然后j指向i-1的位置继续重复这个步骤。不过这里,j只需要前移到floor+1 (上一次找到结果时j所在位置的下一位)的位置。因为,上一次找到结果时,j所在的位置为floor,这一次如果j指向了floor或者更前的位置,则两个字符串的乘积一定比之前的结果要小,因为j和i指向的字符串的长度都比之前要短了。

 bool shorter(const string& a, const string& b)
{
return a.size() < b.size();
}
class Solution {
public:
int maxProduct(vector<string>& words) {
sort(words.begin(), words.end(), shorter);
vector<int> dict(words.size());
for (int i = , n = words.size(); i < n; i++)
for (int j = , len = words[i].size(); j < len; j++)
dict[i] |= ( << (int)(words[i][j] - 'a'));
int floor = -, res = ;
for (int i = words.size() - ; i > floor; i--)
for (int j = i - ; j > floor; j--)
if (!(dict[i] & dict[j]))
{
int l1 = words[i].size(), l2 = words[j].size();
res = max(res, l1 * l2);
floor = j;
break;
}
return res;
}
};

Maximum Product of Word Lengths -- LeetCode的更多相关文章

  1. leetcode 318. Maximum Product of Word Lengths

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

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

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

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

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

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

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

  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】Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  9. LeetCode 318. Maximum Product of Word Lengths (状态压缩)

    题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...

随机推荐

  1. python 3 直接使用reload函数报错

    reload()是python2 的内置函数可以直接使用,但是python3 直接使用此函数报错,需要导入importlib 模块 from importlib import reload

  2. 3D U-Net卷积神经网络

    3D U-Net这篇论文的诞生主要是为了处理一些块状图(volumetric images),基本的原理跟U-Net其实并无大差,因为3D U-Net就是用3D卷积操作替换了2D的,不过在这篇博文中我 ...

  3. linux备忘录-基本命令

    基本命令 将命令分类为获取信息类,文件管理类,目录管理类,文本处理类,系统类,工具类. 获取信息类 uname # 输出所有信息 # 一行输出,空格分割 uname -a # 输出内核名称 uname ...

  4. 通过命令行安装或卸载Tomcat服务

    一.安装Tomcat服务 1.打开命令提示符 方法1: 按住win+R,打开运行,输入cmd,打开命令提示符 方法2:在开始菜单>所有程序>附件>命令提示符 2. 通过命令进入到to ...

  5. ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 I: 寻找大富翁

    http://acm.ocrosoft.com/problem.php?cid=1316&pid=8 题目描述 浙江杭州某镇共有n个人,请找出该镇上的前m个大富翁. 输入 输入包含多组测试用例 ...

  6. SQL SERVER 2008 bug

    我把一个数据的数据导入的到另外一个数据库 作为 测试库使用. 发现里面设置为唯一标识ID  自动增长的表 全部默认是否. 最后只能手动一个个 表全部改过来. 弄了好久才发现这个问题.浪费了我几个小时的 ...

  7. 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...

  8. 修复linux密码

    To reset the root password of your server, you will need to boot into single user mode. Access the M ...

  9. mongo基本命令

    > show dbs    -- 查看数据库列表 > use admin   --创建admin数据库,如果存在admin数据库则使用admin数据库 > db   ---显示当前使 ...

  10. How to secure remote desktop connections using TLS/SSL

    How to secure remote desktop connections using TLS/SSL based authentication Requirement When you ena ...