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. 浅谈 css 之 position用法

    在 css中, position 属性有四个值可用: static(默认值).absolute.relative.fixed. relative:相对定位(相对于自身进行在常规流中的位置进行定位,保留 ...

  2. JavaScript各种数据类型

    (一)JavaScript跟Java.Python等语言一样,也是一门编程语言,配合着html,css等可以让画面动起来, 在页面中导入方式主要有两种,如图 可以自己写在文件里面,一般写在body标签 ...

  3. JavaWeb笔记(九)Ajax&Json

    AJAX 实现方式 原生的JS实现方式 //1.创建核心对象 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, C ...

  4. C#共享内存

    百度:C#共享内存. 文章:C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 资料:UnmanagedMemoryAccessor 资料:MemoryMappe ...

  5. linux运维文章

    运维中关键技术点解剖:1 大量高并发网站的设计方案 :2 高可靠.高可伸缩性网络架构设计:3 网站安全问题,如何避免被黑?4 南北互联问题,动态CDN解决方案:5 海量数据存储架构 一.什么是大型网站 ...

  6. LOJ #124. 除数函数求和 1

    题目描述 $\sigma_k(n) = \sum_{d | n} d ^ k$​ 求 $\sum_{i=1}^n\sigma_k(i)$ 的值对 109 取模的结果. 输入格式 第一行两个正整数 n, ...

  7. 《R语言实战》读书笔记--学习张丹日志

    从张丹的日志(http://blog.fens.me/rhadoop-r-basic/)中第九条对象看到R对象的几个总结: 1.内在属性 mode length 所有对象都有的属性 2.外部属性 at ...

  8. json数据格式的简单案例

    json数据是一种文本字符串,它是javascript的原生数据格式,在数据需要多次重复使用时,json数据是ajax请求的首先.(注:ajax返回的数据格式支持三种分别为:文本格式,json.和xm ...

  9. yum升级kernel

    # uname -a Linux host -.el6.x86_64 # SMP Fri May :: BST x86_64 x86_64 x86_64 GNU/Linux # cat /etc/re ...

  10. 第二届360杯全国大学生信息安全技术大赛部分解题思路(WEB安全)

    第一题如下: 用burpsuit设置好代理后,点击发送验证码,可以看到如下: 然后go之后可以看到如下的验证码: 提交验证码后即可获得key 第二题如下: 通过/data/mysql_error_tr ...