当前位置:网站首页>318. Maximum word length product
318. Maximum word length product
2022-07-01 03:43:00 【Sun_ Sky_ Sea】
318. Maximum word length product
Original title link :https://leetcode.cn/problems/maximum-product-of-word-lengths/
Here's an array of strings words , Find out and return to length(words[i]) * length(words[j]) The maximum of , And these two words don't have a common letter . If there are no such two words , return 0 .
Example 1:
Input :words = [“abcw”,“baz”,“foo”,“bar”,“xtfn”,“abcdef”]
Output :16
explain : These two words are “abcw”, “xtfn”.
Example 2:
Input :words = [“a”,“ab”,“abc”,“d”,“cd”,“bcd”,“abcd”]
Output :4
explain : These two words are “ab”, “cd”.
Example 3:
Input :words = [“a”,“aa”,“aaa”,“aaaa”]
Output :0
explain : There are no such two words .
Tips :
2 <= words.length <= 1000
1 <= words[i].length <= 1000
words[i] Only lowercase letters
Their thinking :
First, remove each string in the array , Use set Gather to get rid of the heavy , The result of de duplication is a set , Convert to string , As in the dictionary key,value Is the length of the string without weight removal . Judge whether the current string intersects with the string in the dictionary , When the length of the de duplicated string is not equal to that of the non duplicated string , Specifically , Small after weight removal , This is the time to judge .
Code implementation :
class Solution:
def maxProduct(self, words: List[str]) -> int:
from collections import defaultdict
words_dict = defaultdict(int)
ans = 0
for word in words:
# Use sets to remove duplicates word
word_set = set(word)
# Convert to string , Used as a dictionary key, think set Not as key, Length as value
# Put the de duplicated string into the dictionary , Used to compare whether two strings coincide
word_set_str = ''.join(word_set)
# If the length of the de duplicated character in the dictionary is smaller than the current character length , Then there may be the maximum value required by the problem
if words_dict[word_set_str] < len(word):
# Ergodic dictionary
for key in words_dict:
# Judge whether there is an intersection between the string in the dictionary and the current de duplicated string
# There is no intersection , In line with the question , Update Max
if not set(key) & word_set:
ans = max(ans, len(word) * words_dict[key])
# Update Dictionary
words_dict[word_set_str] = len(word)
return ans
reference :
https://leetcode.cn/problems/maximum-product-of-word-lengths/solution/pythonjavajavascriptgo-zi-zhi-hashable-s-tuxj/
边栏推荐
- Database DDL (data definition language) knowledge points
- GCC usage, makefile summary
- [TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
- 小程序容器技术与物联网IoT的结合点
- FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
- 排序链表(归并排序)
- Feature Pyramid Networks for Object Detection论文理解
- Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
- Edlines: a real time line segment detector with a false detection control
- 后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
猜你喜欢

小程序容器技术与物联网IoT的结合点

The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list

Valentine's Day is nothing.

Feature pyramid networks for object detection

pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear

Cygwin的下载和安装配置

Test function in pychram

谷粒学院微信扫码登录过程记录以及bug解决

【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍

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
随机推荐
Listener listener
Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
Filter
Explain spark operation mode in detail (local+standalone+yarn)
Unexpected token o in JSON at position 1 ,JSON解析问题
208. 实现 Trie (前缀树)
Develop industrial Internet with the technical advantages of small programs
在 C 中声明函数之前调用函数会发生什么?
10、Scanner. Next() cannot read spaces /indexof -1
【伸手党福利】开发人员重装系统顺序
4、【WebGIS实战】软件操作篇——数据导入及处理
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
【TA-霜狼_may-《百人计划》】2.1 色彩空间
【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
[party benefits] jsonobject to string, leave blank
165. compare version numbers
排序链表(归并排序)
318. 最大单词长度乘积
242. 有效的字母异位词
Pytorch training deep learning network settings CUDA specified GPU visible