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.

此题如果用暴力比较法,也就是两层循环依次比较的话,执行会超时。

可以通过类似C++中bitset的方法来保存每个单词的一个key值,然后直接用key值进行比较,减少单词之间比较的时候比较字符的时间。

比如单词abcw,我们可以创建一个l = [0,0,0.....0] 有27个0的数组,并按26个字母的顺序依次给a-z索引为1~26,最后把a,b,c,w四位对应的元素的值置为1,计算 pow(2,1)+pow(2,2)+pow(2,3)+pow(2,23)的和即为这个元素的key值。

再用这个key值与其他元素的key值做与操作,结果为0,则表示单词无相同的字符。

下面是代码:

class Solution(object):
index = []
def transChar(self,c):
l = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
return l.index(c) + 1
def parseWords(self,w):
t = 0
l = []
for i in range(27):
l.append(0)
for i in set(w): #注:这里用set过滤掉相同的字符,我最初直接用w,导致运行超时
t = self.transChar(i)
l[t] = 1
t = 0
for i in range(len(l)):
if l[i] == 1:
t = t + pow(2,i)
#print w,t
return t
def maxProduct(self, words):
"""
:type words: List[str]
:rtype: int
""" max = 0
if len(words) == 0:
return 0
l = []
for i in words:
l.append(self.parseWords(i)) for i in range(len(l)):
for j in range(i+1,len(l)):
if l[i] & l[j] == 0:
if max < len(words[i]) * len(words[j]):
max = len(words[i]) * len(words[j])
return max

【leetcode】Maximum Product of Word Lengths的更多相关文章

  1. leetcode 318. Maximum Product of Word Lengths

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

  2. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  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. 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单词长度最大乘积

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

  6. 【Leetcode】Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

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

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

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

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

  9. 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)

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

随机推荐

  1. 语言模型评价指标Perplexity

    在信息论中,perplexity(困惑度)用来度量一个概率分布或概率模型预测样本的好坏程度.它也可以用来比较两个概率分布或概率模型.(应该是比较两者在预测样本上的优劣)低困惑度的概率分布模型或概率模型 ...

  2. [转帖]微软 SQL Server 2008/R2 停止支持

    微软 SQL Server 2008/R2 停止支持 微软停止支持 SQLSERVER 2008R2 https://t.cj.sina.com.cn/articles/view/3172142827 ...

  3. Two strings CodeForces - 762C (字符串,双指针)

    大意: 给定字符串$a$,$b$, $b$可以任选一段连续的区间删除, 要求最后$b$是$a$的子序列, 求最少删除区间长度. 删除一段连续区间, 那么剩余的一定是一段前缀和后缀. 判断是否是子序列可 ...

  4. RAD Studio 10 up1欢迎页证书不可用

    不只是欢迎页,每打开一个新的工程,都会出现上面那个证书不可用的提示. 解决方法: 1.通过Fiddler网络监控软件分析发现,出现这个问题的原因是bds启动的时候会用https协议访问谷歌的统计服务器 ...

  5. 渗透神器CobaltStrike 3.1.2 去后门破解版 & Windows版TeamServer【转】

    转自我八师傅博客 CS简介 Cobalt Strike(简称CS)是全球黑客公认一款非常优秀的渗透测试神器,以metasploit为基础的GUI的框架式渗透工具,集成了传统远控功能(远程桌面VNC.键 ...

  6. zabbix常见错误处理方式

    1.zabbix-server启动失败,我是centos7.4系统,查看/var/log/zabbix/zabbix_server.log日志显示 1516:20180816:195630.755 u ...

  7. SpringBoot核心特性之组件自动装配

    写在前面 spring boot能够根据依赖的jar包自动配置spring boot的应用,例如: 如果类路径中存在DispatcherServlet类,就会自动配置springMvc相关的Bean. ...

  8. IO模型(epoll)--详解-01

    写在前面 从事服务端开发,少不了要接触网络编程.epoll作为linux下高性能网络服务器的必备技术至关重要,nginx.redis.skynet和大部分游戏服务器都使用到这一多路复用技术. 本文会从 ...

  9. 14Filter&Listener

    1.Filter:过滤器 1.1 概念 生活中的过滤器:净水器,空气净化器,土匪. web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成一些特殊的功能. 过滤器的作用: 一般用于完成 ...

  10. 08ServletContext

    1. 概念 代表整个web应用,可以和程序的容器(服务器)来通信 2. 获取 1. 通过request对象获取 request.getServletContext();2. 通过HttpServlet ...