当前位置:网站首页>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/
边栏推荐
猜你喜欢
数据库中COMMENT关键字的使用
Nacos
Gorilla/mux framework (RK boot): RPC error code design
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
pytorch训练深度学习网络设置cuda指定的GPU可见
IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
文件上传下载
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
Error: plug ins declaring extensions or extension points must set the singleton directive to true
随机推荐
JS daily development tips (continuous update)
E15 solution for cx5120 controlling Huichuan is620n servo error
5、【WebGIS实战】软件操作篇——服务发布及权限管理
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
网页不能右键 F12 查看源代码解决方案
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
排序链表(归并排序)
Leetcode 128 longest continuous sequence (hash set)
整合阿里云短信的问题:无法从静态上下文中引用非静态方法
How do I use Google Chrome 11's Upload Folder feature in my own code?
用小程序的技术优势发展产业互联网
Design of serial port receiving data scheme
Error: plug ins declaring extensions or extension points must set the singleton directive to true
4. [WebGIS practice] software operation chapter - data import and processing
BluePrism注册下载并安装-RPA第一章
数据交换 JSON
LeetCode 128最长连续序列(哈希set)
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
Bilinear upsampling and f.upsample in pytorch_ bilinear
Pyramid Scene Parsing Network【PSPNet】论文阅读