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
相关推荐
全栈练习生29 分钟前
大模型原理之 KV Cache
python·ai
天赐范式41 分钟前
天赐范式第183天:让选择开始生效——育种者方程与选择生效条件
python·数字生命·天赐范式·动态运行时·育种者方程·选择压力·遗传力
benchmark_cc1 小时前
批量行情返回后,怎样把请求失败的股票和正常数据分开?
python·数据分析·pandas·量化交易·股票数据·quantdash
y = xⁿ2 小时前
关于Agent工程落地
前端·人工智能·python
小小张说故事2 小时前
pip 安装慢、超时、报错?一份国内源配置 + 9 个高频报错对照表
python
在世修行2 小时前
干货:长任务反馈
python·长任务
东方芷兰2 小时前
Agent 技术摘要 05 —— BP、SaaS、B2B、C2B、B2C
java·笔记·python·语言模型·自然语言处理
老歌老听老掉牙3 小时前
麻花钻切削过程的力学与效率解析
python·钻头·切削
happylifetree3 小时前
Python27:核心语法-流程控制语句-match模式匹配
python
奋进的LY3 小时前
spacy的安装和使用教程
人工智能·python·深度学习·github