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

  1. class Solution(object):
  2. def maxProduct(self, words):
  3. bits_words = [reduce(lambda s,x: s|(1<<ord(x)-ord('a')), w, 0) for w in words]
  4. ans = 0
  5. for i, word in enumerate(words):
  6. for j in range(i+1, len(words)):
  7. if len(word)*len(words[j]) > ans and self.has_diff(bits_words[i], bits_words[j]):
  8. ans = len(word)*len(words[j])
  9. return ans
  10.  
  11. def has_diff(self, w1, w2):
  12. return (w1&w2) == 0

318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示的更多相关文章

  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】

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  3. 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. 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 (Bit Manipulations)

    https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...

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

  7. [LC] 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. 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)

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

  9. 318 Maximum Product of Word Lengths 最大单词长度乘积

    给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...

随机推荐

  1. hdu 2199 (二分)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 ...

  2. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  3. jQuery EasyUI DataGrid API 中文文档

        扩展自$.fn.panel.defaults,用 $.fn.datagrid.defaults重写了 defaults . 依赖 panel resizable linkbutton pagi ...

  4. JSON的操作

    今天遇到了一个要解析JSON的需求.在http://stackoverflow.com/questions/1826727/how-do-i-parse-json-with-ruby-on-rails ...

  5. 对List中对象的去重

    今天项目中遇到了一个对List中对象去重的问题. 首先对于我们自己系统中的对象我们只要重写该对象的 equal 和 hashcode 即可(利用对象中的能够唯一确定对象的属性). 但是我遇到的不是本系 ...

  6. 基于Spark ALS构建商品推荐引擎

    基于Spark ALS构建商品推荐引擎   一般来讲,推荐引擎试图对用户与某类物品之间的联系建模,其想法是预测人们可能喜好的物品并通过探索物品之间的联系来辅助这个过程,让用户能更快速.更准确的获得所需 ...

  7. Python学习(5)条件语句

    目录 Python 条件语句 Python 简单的语句组 Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了 ...

  8. TextView使用SpannableString设置复合文本

    TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来对指定文本进行相关处理,具体有以下功能: 1.Bac ...

  9. 使用==比较String类型

    String类型的比较 public class StringDemo { public static void main(String[] args) { String s1 = "abc ...

  10. js 删除多个相同name元素。

    var obj = document.getElementsByName("abc"); for(var i = 0;i<(obj.length) * 2;i++){ obj ...