传送门

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. 立个单调栈flag

    http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=687&pid=1002

  2. idea报错:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configu

    java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more tha ...

  3. numpy.random.randint

    low.high.size三个参数.默认high是None,如果只有low,那范围就是[0,low).如果有high,范围就是[low,high). >>> np.random.ra ...

  4. 快学UiAutomator UiDevice API 详解

    一.按键使用 返回值 方法名 说明 boolean pressBack() 模拟短按返回back键 boolean pressDPadCenter() 模拟按轨迹球中点按键 boolean press ...

  5. 解决微信小程序要求的TLS版本必须大于等于1.2的问题(windows2008服务器)

    开始->运行->输入 PowerShell 复制这段代码粘入弹出的dos窗口内 # Enables TLS 1.2 on windows Server 2008 R2 and Window ...

  6. 分享一个C++与Python开发的中小型通用游戏服务端框架(跨平台,开源,适合MMORPG游戏)

    在开发一款游戏项目时,在立项时我们往往会考虑或者纠结很多,比如: 1,对于开发来说:服务端和客户端应该选择什么语言?用什么协议通信才更效率?协议后期如何维护?Socket是用长连接还是短连接?TCP还 ...

  7. Bzoj 1085: [SCOI2005]骑士精神 (dfs)

    Bzoj 1085: [SCOI2005]骑士精神 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 dfs + 剪枝. 剪枝方法: ...

  8. 【线段树】uoj#228. 基础数据结构练习题

    get到了标记永久化 sylvia 是一个热爱学习的女孩子,今天她想要学习数据结构技巧. 在看了一些博客学了一些姿势后,她想要找一些数据结构题来练练手.于是她的好朋友九条可怜酱给她出了一道题. 给出一 ...

  9. MySQL 查询优化之 Index Merge

    MySQL 查询优化之 Index Merge Index Merge Intersection 访问算法 Index Merge Union 访问算法 Index Merge Sort-Union ...

  10. webpack hmr

    参考: https://webpack.js.org/concepts/hot-module-replacement/ https://webpack.js.org/guides/hot-module ...