3.18 最大单词长度——力扣

318. 最大单词长度乘积

已解答

中等

相关标签

相关企业

给你一个字符串数组 words ,找出并返回 length(words[i]) * length(words[j]) 的最大值,并且这两个单词不含有公共字母。如果不存在这样的两个单词,返回 0

示例 1:

复制代码
输入:words = ["abcw","baz","foo","bar","xtfn","abcdef"]
输出:16 
解释:这两个单词为"abcw", "xtfn"。

示例 2:

复制代码
输入:words = ["a","ab","abc","d","cd","bcd","abcd"]
输出:4 
解释:这两个单词为 "ab", "cd"。

示例 3:

复制代码
输入:words = ["a","aa","aaa","aaaa"]
输出:0 
解释:不存在这样的两个单词。

提示:

  • 2 <= words.length <= 1000
  • 1 <= words[i].length <= 1000
  • words[i] 仅包含小写字母

python

python 复制代码
class Solution(object):
    def maxProduct(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        lists = {}
        for i in words:
            lists[i] = [len(i), set(list(i))]
        ans = 0
        lens = len(words)
        for i in range(lens):
            si=lists[words[i]]
            for j in range(i + 1, lens):
                sj=lists[words[j]]
                if not si[1].intersection(sj[1]) and si[0] * sj[0] > ans:
                    ans = si[0] * sj[0]
        return ans
相关推荐
xcLeigh2 分钟前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh3 分钟前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
YongCheng_Liang23 分钟前
从零开始学 Python:自动化 / 运维开发实战(核心库 + 3 大实战场景)
python·自动化·运维开发
鸽芷咕34 分钟前
为什么越来越多开发者转向 CANN 仓库中的 Python 自动化方案?
python·microsoft·自动化·cann
秋邱35 分钟前
用 Python 写出 C++ 的性能?用CANN中PyPTO 算子开发硬核上手指南
开发语言·c++·python
wazmlp0018873691 小时前
python第三次作业
开发语言·python
深蓝电商API2 小时前
住宅代理与数据中心代理在爬虫中的选择
爬虫·python
历程里程碑3 小时前
普通数组----合并区间
java·数据结构·python·算法·leetcode·职场和发展·tornado
weixin_395448913 小时前
mult_yolov5_post_copy.c_cursor_0205
c语言·python·yolo
执风挽^3 小时前
Python基础编程题2
开发语言·python·算法·visual studio code