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

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.

class Solution {
public: void strToBits(vector<int>& bits, vector<string>& words) {
for(int i=; i<words.size(); ++i) {
int tmp = ;
for(int j=; j<words[i].size(); ++j) {
int offset = words[i][j] - 'a';
tmp = (tmp | ( << offset));
}
bits[i] = tmp;
}
} int maxProduct(vector<string>& words) {
int n = words.size();
if(n < ) return ; vector<int> bits(n, );
strToBits(bits, words); int res = ;
for(int i=; i<n; ++i) {
for(int j=i+; j<n; ++j) {
int len1 = words[i].length(), len2 = words[j].length();
if((bits[i] & bits[j]) == ) {
res = max(res, len1 * len2);
}
}
} return res;
}
};

leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)的更多相关文章

  1. leetcode 318. Maximum Product of Word Lengths

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

  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. ActivePython2.7 +Firefly1.2.2+WIN7服务器搭建过程(已通过)

    原地址:http://www.9miao.com/question-15-54027.html 1.ActivePython2.7 版本(内部已经包含easy_install,pywin32)2.所需 ...

  2. DAO是什么技术

    DAO是Data Access Object数据访问接口,数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据库资源中间. 在核心J2EE模式中是这样介绍DAO模式的:为了建立一个健壮的J2EE应 ...

  3. C#基础精华06(Linq To XML,读取xml文件,写入xml)

    1.XML概述: 可扩展标记语言XML(eXtensible Markup Language)是一种简单灵活的文本格式的可扩展标记语言,侧重于存储数据. 2.XML特点 xml 标记语言 html x ...

  4. SGU 101 AC

    总算AC了,好帅气的算法,同时解决了自环跟非连通,一种自下向上找环的算法过程,这里把欧拉回路讲得很清楚,赞. #include <iostream> #include <vector ...

  5. Android ImageView(纯java)

    import android.app.Activity; import android.graphics.*; import android.graphics.drawable.*; import a ...

  6. Windbg:如何给字符串下条件断点

    因为Windgb支持MASM语法,字符串的比较方法有$scmp和$sicmp.用法和c中的字符串比较方法一致.在需要比较字符串成员变量的时候,遇到了点问题.因为字符串成员变量无法直接获取字符串内容.p ...

  7. python学习笔记七--数据操作符的优先级

    一.混合表达式哪个部分先计算: 1. 取决于操作符的优先级,不同操作符的优先级是不一样的,例如‘*’的优先级高于‘+’ 2. 上节的表格里越靠后,优先级越高. 3 . 同一表达式的相同操作符是按从左到 ...

  8. Zookeeper基本知识

    Zookeeper的Session: (1)客户端和server间采用长连接 (2)连接建立后,server产生session ID(64位)返还给客户端 (3)客户端定期发送ping包来检查和保持和 ...

  9. 1521. War Games 2(线段树解约瑟夫)

    1521 根据区间和 来确定第k个数在哪 #include <iostream> #include<cstdio> #include<cstring> #inclu ...

  10. jquery网页倒计时效果,秒杀,限时抢购!

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...