作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/maximum-product-of-word-lengths/description/

题目描述

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:

Input: ["abcw","baz","foo","bar","xtfn","abcdef"]
Output: 16
Explanation: The two words can be "abcw", "xtfn".

Example 2:

Input: ["a","ab","abc","d","cd","bcd","abcd"]
Output: 4
Explanation: The two words can be "ab", "cd".

Example 3:

Input: ["a","aa","aaa","aaaa"]
Output: 0
Explanation: No such pair of words.

题目大意

找出两个不包含重复字符的字符串长度乘积最大值。

解题方法

set

重点是不包含重复字符,显然可以用set统计每个字符串中出现的字符,然后利用O(n^2)的时间复杂度暴力求解,竟然过了!

Python代码如下:

class Solution:
def maxProduct(self, words):
"""
:type words: List[str]
:rtype: int
"""
word_dict = dict()
for word in words:
word_dict[word] = set(word)
max_len = 0
for i1, w1 in enumerate(words):
for i2 in range(i1+1, len(words)):
w2 = words[i2]
if not (word_dict[w1] & word_dict[w2]):
max_len = max(max_len, len(w1) * len(w2))
return max_len

位运算

这个是个巧妙的方法。我们知道int有32位,而英文小写字符只有26个,所以,对于一个字符串,把其出现过的字符对应到int上去,那么这个int就能当做这个字符串的摘要,表示这个这个字符串中都有哪些字符。

我们把每个字符串都形成一个摘要,这样只要两个字符串的摘要想与之后的结果是0,那么说明两个字符串没有公共字符。

是不是感觉这个方法似曾相识呢?没错,这个很类似布隆过滤器啊!

python代码如下:

class Solution(object):
def maxProduct(self, words):
"""
:type words: List[str]
:rtype: int
"""
res = 0
d = collections.defaultdict(int)
N = len(words)
for i in range(N):
w = words[i]
for c in w:
d[w] |= 1 << (ord(c) - ord('a'))
for j in range(i):
if not d[words[j]] & d[words[i]]:
res = max(res, len(words[j]) * len(words[i]))
return res

参考资料:http://www.cnblogs.com/grandyang/p/5090058.html

日期

2018 年 7 月 5 日 —— 天气变化莫测呀,建议放个伞
2019 年 3 月 23 日 —— 坚持刷题

【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)的更多相关文章

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

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

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

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

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

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

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

  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. vc控制台程序关闭事件时的正确处理方式

    百度可以找到很多关于这个问题解决的方法 关键控制台API函数:SetConsoleCtrlHandler 在支持C++ 11以上的编译器中,你可以这么做. SetConsoleCtrlHandler( ...

  2. DOM给表格添加新一行和删除整个行的内容

    DOM用appendChild()给表格添加新一行时,要注意,在HTML中没特别设置<thead>,<tbody>时,会自动添加上,所以要选择表格第一个元素在添加tr. // ...

  3. 华为AppTouch携手全球运营商,助力开发者出海

    内容来源:华为开发者大会2021 HMS Core 6 APP services技术论坛,主题演讲<华为AppTouch携手全球运营商,助力开发者出海>. 演讲嘉宾:华为消费者云服务App ...

  4. 日常Java测试第二段 2021/11/12

    第二阶段 package word_show; import java.io.*;import java.util.*;import java.util.Map.Entry; public class ...

  5. Vue相关,vue父子组件生命周期执行顺序。

    一.实例代码 父组件: <template> <div id="parent"> <child></child> </div& ...

  6. Java SSLSocket

    Java SSLSocket JSSE(Java Security Socket Extension)是Sun公司为了解决互联网信息安全传输提出的一个解决方案,它实现了SSL和TSL协议,包含了数据加 ...

  7. 【Git项目管理】Git分支 - 远程分支

    远程分支 远程引用是对远程仓库的引用(指针),包括分支.标签等等. 你可以通过 git ls-remote (remote) 来显式地获得远程引用的完整列表,或者通过 git remote show ...

  8. OS开发之Objective-C与JavaScript的交互

    UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS ...

  9. RHEL 6.5安装系统

    转自如下链接: http://www.jb51.net/os/128752.html

  10. Vue.js 学习

    一,Vue.js 介绍 Vue 是一套用于构建用户界面的渐进式javascript框架,与其它大型框架不同的是:Vue被设计为可以自底向上逐层应用.Vue的核心库只关注视图层,不仅易于上手,还便于与第 ...