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.

思路:对每一个元素都进行掩码设置,因为一共就26个字母,所以1个int的bit位就可以代表一个字母,比如"a"就可以表示成0000 0000 0000 0000 0000 0000 0000 0001,"bc"可以表示成0000 0000 0000 0000 0000 0000 0000 0110;如果两个元素的掩码位与以后结果为0,表示他们之间没有相同的字母,则乘积就是两串字符串的长度的乘积。

代码

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

  

  

  

LeetCode 【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 解题报告(Python)

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

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

  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. LeetCode 318. Maximum Product of Word Lengths (状态压缩)

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

  7. Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算

    先介绍下本题的题意: 在一个字符串组成的数组words中,找出max{Length(words[i]) * Length(words[j]) },其中words[i]和words[j]中没有相同的字母 ...

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

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

随机推荐

  1. JavaScript toFixed 用法

    在JS中进行乘法运算时 我们又是会遇到一下情况 <input type="text" id="num1" value="" /> ...

  2. Sql 常用时间转换

    CONVERT(varchar(100), GETDATE(), 0); -- 08 31 2015 04:57PM CONVERT(varchar(100), GETDATE(), 20); --2 ...

  3. Gnu/Linux的学习探索

    1.Gnu/Linux是一个基于POSIX和UNIX的多用户多任务 支持多线程多CPU的类UNIX的操作系统. 继承了UNIX以网络为核心的设计思想 是性能稳定的多用户网络操作系统. 1991年10月 ...

  4. 对客户推荐产品模型+python代码

    首先观看数据: l  数据的基本特征用  describe  描述每个基本特征 l  画图画出每个特征的基本统计图 应用import matplotlib.pylab as pl  画图显示 l  关 ...

  5. 使用共享网卡的NAT模式配置VMware中的CentOS的上网功能

    昨天写了一篇文章总结了前两天折腾VMware 10中的CentOS上网的问题,结果留下一下小瑕疵,就是视频教程中通过共享网卡使用NAT模式配置虚拟机的方法.今天在结合昨天的基础上终于弄明白了这个问题. ...

  6. Android深度探索--HAL与驱动开发----第二章读书笔记

    1. 底层开发工具包括: JDk6或者以上版本:Eclipse3.4或以上版本:ADT(用于开发Android应用程序),CDT(用于开发AndroidNDK程序):Android SDK:Andro ...

  7. JavaScript数组模拟栈和队列

    *栈和队列:js中没有真正的栈和队列的类型              一切都是用数组对象模拟的 栈:只能从一端进出的数组,另一端封闭       FILO   何时使用:今后只要仅希望数组只能从一端进 ...

  8. Web测试中常见分享问题

         Web测试中,由于开发通常指注重完成H5页面的逻辑功能,对各种系统.浏览器等考虑不周,同时Android端各类机型碎片化,容易产生兼容性问题,这其中以分享类型为最. 本文简单分析总结一些测试 ...

  9. WCF Restful 服务器配置文件

    <?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...

  10. oracle xmltype导入并解析Excel数据 (一)创建表与序

    表说明: T_EXCEL_IMPORT_DATASRC: Excel数据存储表,(使用了xmltype存储Excel数据) 部分字段说明: BUSINESSTYPE: Excel模板类型,一个Exce ...