当前位置:网站首页>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/
边栏推荐
- 完全背包问题
- 241. 为运算表达式设计优先级
- Listener listener
- bootsrap中的栅格系统
- 214. 最短回文串
- 6. Z 字形变换
- 小程序容器技术与物联网IoT的结合点
- 165. 比较版本号
- 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
- How do I use Google Chrome 11's Upload Folder feature in my own code?
猜你喜欢
![[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation](/img/b3/887d3fb64acbf3702814d32e2e6414.png)
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation

Asgnet paper and code interpretation 2

Cygwin的下载和安装配置

【TA-霜狼_may-《百人计划》】2.3 常用函数介绍

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)

用小程序的技术优势发展产业互联网

排序链表(归并排序)
![Pyramid scene parsing network [pspnet] thesis reading](/img/05/4645c8a595083479dee6835620335d.png)
Pyramid scene parsing network [pspnet] thesis reading

【TA-霜狼_may-《百人计划》】2.1 色彩空间

Avalanche problem and the use of sentinel
随机推荐
MFC窗口滚动条用法
ASGNet论文和代码解读2
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
Gorilla/mux framework (RK boot): RPC error code design
Feign remote call and getaway gateway
Pyramid scene parsing network [pspnet] thesis reading
Cookie&Session
数据库中COMMENT关键字的使用
How do I use Google Chrome 11's Upload Folder feature in my own code?
Research on target recognition and tracking based on 3D laser point cloud
JS daily development tips (continuous update)
不用加减乘除实现加法
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
torch. histc
Bilinear upsampling and f.upsample in pytorch_ bilinear
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
165. 比较版本号
What happens when a function is called before it is declared in C?