当前位置:网站首页>318. 最大单词长度乘积
318. 最大单词长度乘积
2022-07-01 03:23:00 【Sun_Sky_Sea】
318. 最大单词长度乘积
原始题目链接:https://leetcode.cn/problems/maximum-product-of-word-lengths/
给你一个字符串数组 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] 仅包含小写字母
解题思路:
先去重数组中每个字符串,使用set集合去重,去重后的结果是个集合,转换成字符串,当做字典中的key,value是没去重的字符串的长度。判断当前字符串和字典中的字符串是否有交集,当去重后的字符串和没去重的字符串的长度不相等的时候,具体说是,去重后的小,这个时候判断。
代码实现:
class Solution:
def maxProduct(self, words: List[str]) -> int:
from collections import defaultdict
words_dict = defaultdict(int)
ans = 0
for word in words:
# 使用集合去重word
word_set = set(word)
# 转换成字符串,用做字典的key,以为set不能当做key,长度作为value
# 将去重后的字符串放进字典中,用于比较两个字符串是否有重合
word_set_str = ''.join(word_set)
# 如果字典中的去重后的字符长度比当前字符长度小,则有可能存在题目要求的最大值
if words_dict[word_set_str] < len(word):
# 遍历字典
for key in words_dict:
# 判断字典中的字符串和当前去重后的字符串是否有交集
# 没有交集,符合题意,更新最大值
if not set(key) & word_set:
ans = max(ans, len(word) * words_dict[key])
# 更新字典
words_dict[word_set_str] = len(word)
return ans
参考文献:
https://leetcode.cn/problems/maximum-product-of-word-lengths/solution/pythonjavajavascriptgo-zi-zhi-hashable-s-tuxj/
边栏推荐
- 岭回归和lasso回归
- shell脚本使用两个横杠接收外部参数
- go实现命令行的工具cli
- Ouc2021 autumn - Software Engineering - end of term (recall version)
- The difference between MFC for static libraries and MFC for shared libraries
- 【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
- Ctfshow blasting WP
- Pytorch training deep learning network settings CUDA specified GPU visible
- What happens when a function is called before it is declared in C?
- Overview of EtherCAT principle
猜你喜欢

LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表

Thread data sharing and security -threadlocal

Use of comment keyword in database

JUC learning

Nacos

The difference between MFC for static libraries and MFC for shared libraries

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management

BluePrism注册下载并安装-RPA第一章

Detailed list of errors related to twincat3 ads of Beifu
随机推荐
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Develop industrial Internet with the technical advantages of small programs
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
MFC窗口滚动条用法
Appium fundamentals of automated testing - basic principles of appium
【伸手党福利】开发人员重装系统顺序
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
E15 solution for cx5120 controlling Huichuan is620n servo error
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
Force buckle - sum of two numbers
The difference between MFC for static libraries and MFC for shared libraries
Promise中finally的用法
二叉树神级遍历:Morris遍历
在 C 中声明函数之前调用函数会发生什么?
Thread data sharing and security -threadlocal
4、【WebGIS实战】软件操作篇——数据导入及处理
Feign remote call and getaway gateway
Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
[daily training] 1175 Prime permutation