传送门

318. Maximum Product of Word Lengths

My Submissions

QuestionEditorial Solution
Total Accepted: 19855 Total Submissions: 50022 Difficulty: Medium

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.

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Tags

Bit Manipulation

 
 
题解:
 
 find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters.
重点在于 判断 有没有重复的字母
由于只有小写字母(26个),所以用 状压,位运算 (与)即可
 
 
 class Solution {
public:
int maxProduct(vector<string>& words) {
int n = words.size();
vector <int> len;
vector <int> contain;
int i,j;
int l;
for(i = ;i < n;i++){
l = words[i].length();
len.push_back(l);
int tmp = ;
for(j = ;j < l;j++){
int x = words[i][j] - 'a';
tmp |= ( << x);
}
contain.push_back(tmp);
}
int re = ;
for(i = ;i < n;i++){
for(j = i + ;j < n;j++){
if(contain[i] & contain[j]){
continue;
}
re = max(re,len[i] * len[j]);
}
}
return re;
}
};

leetcode 318. Maximum Product of Word Lengths的更多相关文章

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

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

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

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

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

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

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

  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. 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. UVA - 11082 Matrix Decompressing (最大流,技巧)

    很经典的网络流模型,行编号和列编号分别看成一个点,行和列和分别看出容量,一个点(x,y)看出是一条边,边的容量下界是1,所以先减去1,之后在加上就好了. 建图的时候注意分配好编号,解从残留网络中的边找 ...

  2. java多线程---ReentrantLock源码分析

    ReentrantLock源码分析 基础知识复习 synchronized和lock的区别 synchronized是非公平锁,无法保证线程按照申请锁的顺序获得锁,而Lock锁提供了可选参数,可以配置 ...

  3. 操作系统项目:向Linux内核添加一个系统调用

    内容: 向Linux增加一个系统调用 撰写一个应用测试程序调用该系统调用 使用ptrace或类似的工具对该测试程序进行跟踪调 环境: 1.vmware workstation 15.0.0 2.ubu ...

  4. common-fileupload上传文件

    文件上传在web应用中非常普遍,要在jsp环境中实现文件上传功能是非常容易的,因为网上有许多用java开发的文件上传组件,本文以commons-fileupload组件为例,为jsp应用添加文件上传功 ...

  5. 118. Pascal's Triangle@python

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. Example: Inpu ...

  6. WebAssembly MDN简单使用

    MDN 就是通过编译器编译完成c后生成的胶水代码 引入js 就能直接调用定义在c或者c++中的函数了 c代码如下: #include <stdio.h> #include <stdl ...

  7. windows10锁定屏幕聚焦图片导出

    打开运行,输入%LocalAppData%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Ass ...

  8. 如何用纯 CSS 创作一个均衡器 loader 动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/oybWBy 可交互视频教 ...

  9. python基础学习笔记——生成器与推导式

    生成器 首先我们来看看什么是个生成器,生成器本质就是迭代器 在python中有三种方式来获取生成器 1.通过生成器函数 2.通过各种推到式来实现生成器 3.通过数据的转换也可以获取生成器 首先,我们先 ...

  10. checkStyle使用手册

    1. Annotations(注解:5个) Annotation Use Style(注解使用风格) 这项检查可以控制要使用的注解的样式. Missing Deprecated(缺少deprecad) ...