传送门

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. Resize a UIImage the right way

    When deadlines loom, even skilled and experienced programmers can get a little sloppy. The pressure ...

  2. 2006: C语言实验——拍皮球

    2006: C语言实验——拍皮球 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 231  Solved: 162[Submit][Status][Web ...

  3. fclose - 关闭流

    SYNOPSIS 总览 #include <stdio.h> int fclose(FILE *stream); DESCRIPTION 描述 函数 fclose 将名为 stream 的 ...

  4. Bootstrap历练实例:基本按钮群组

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. 【Java_基础】Java的访问权限控制

    1.类成员的访问权限控制 Java中类成员的访问权限分为四类:private,无(默认情况下),protected和public.其权限控制如下表所示: 修饰词 本类 同一个包的类 继承类 其他类 p ...

  6. mybatis 批量操作增删改查

    在介绍批量操作之前,首先先介绍一个语法:foreach.可以说是,foreach是整个批量操作的灵魂. 属性 描述 item 循环体中的具体对象. 支持属性的点路径访问,如item.age,item. ...

  7. centOS下jenkins

    转:centos7搭建jenkins小记 转自:https://segmentfault.com/a/1190000007086764 安装java环境 1.查看服务器版本 centos7,继续. c ...

  8. 《嵌入式linux应用程序开发标准教程》笔记——9.多线程编程

    线程是轻量级进程,创建线程的开销要比进程小得多,在大型程序中应用广泛. 9.1 线程概述 进程包含自己的代码.数据.堆栈.资源等等,创建和切换的开销比较大: 线程是轻量级的进程,调度的最小单元,同一个 ...

  9. windows中Python多版本与jupyter notebook中使用虚拟环境

    本人电脑是windows系统,装了Python3.7版本,但目前tensorflow支持最新的python版本为3.6,遂想再安装Python3.6以跑tensorflow. 因为看极客时间的专栏提到 ...

  10. python爬虫入门三:requests库

    urllib库在很多时候都比较繁琐,比如处理Cookies.因此,我们选择学习另一个更为简单易用的HTTP库:Requests. requests官方文档 1. 什么是Requests Request ...