给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
示例 1:
输入 ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
返回 16
两个单词可以为 "abcw", "xtfn"。
示例 2:
输入 ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
返回 4
两个单词可以为 "ab", "cd"。
示例 3:
输入 ["a", "aa", "aaa", "aaaa"]
返回 0
没有这样的两个单词。

详见:https://leetcode.com/problems/maximum-product-of-word-lengths/description/

class Solution {
public:
int maxProduct(vector<string>& words) {
int res=0;
vector<int> mask(words.size(),0);
for(int i=0;i<words.size();++i)
{
for(char c:words[i])
{
mask[i]|=1<<(c-'a');
}
for(int j=0;j<i;++j)
{
if(!(mask[i]&mask[j]))
{
res=max(res,int(words[i].size()*words[j].size()));
}
}
}
return res;
}
};

参考:http://www.cnblogs.com/grandyang/p/5090058.html

318 Maximum Product of Word Lengths 最大单词长度乘积的更多相关文章

  1. leetcode 318. Maximum Product of Word Lengths

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

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

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

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

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

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

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

  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. [LC] 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 ...

随机推荐

  1. Vuex实践小记

    1.目录结构 2.开始(安装vuex) npm install vuex --save 3.编辑store/index.js(创建一个Vuex.store状态管理对象) import Vue from ...

  2. 洛谷 2574 XOR的艺术

    [题解] 线段树维护区间中1的个数就好了.每次修改就打上标记并把区间的sum改为len-sum. #include<cstdio> #include<algorithm> #i ...

  3. CSS3制作404立体字体

    CSS3制作404立体字体页面效果     鼠标移动上去,背景色变白.       动态效果:     .demo p:first-child span:hover { text-shadow:0px ...

  4. App后台开发运维和架构实践学习总结(5)——App产品从需求到研发到开发到上线到产品迭代全过程

    前言 如果没有做过开发,研发过产品的人,很难体会做产品的艰难,刚进公司的人,一般充当的是程序开发,我这里说的是开发,它与研发是有区别的. 一个需求下来,如果不能很好地理解产品需求,如果不能很好的驾驭需 ...

  5. 树上启发式合并(DSU on tree)

    //heavy-light decomposition style .//http://codeforces.com/blog/entry/44351 int cnt[maxn]; bool big[ ...

  6. android中listview点击监听器onItemClick四个参数的含义

    public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) X, Y两个listvie ...

  7. nyoj_116_士兵杀敌(二)_201404131107

    士兵杀敌(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常 ...

  8. 18、Java并发性和多线程-饥饿与公平

    以下内容转自http://ifeve.com/starvation-and-fairness/: 如果一个线程因为CPU时间全部被其他线程抢走而得不到CPU运行时间,这种状态被称之为“饥饿”.而该线程 ...

  9. SiteMesh2-sitemesh.xml的ParameterDecoratorMapper映射器的用法

    继续使用上一章http://www.cnblogs.com/EasonJim/p/7086916.html的例子,改造成使用ParameterDecoratorMapper映射器的方法,这个映射器不需 ...

  10. 配置类Configuration怎样使用

    public class CorsConfiguration { /** * Wildcard representing <em>all</em> origins, metho ...