当前位置:网站首页>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/
边栏推荐
- 166. 分数到小数
- [TA frost wolf \u may- hundred people plan] 1.3 secret of texture
- Edlines: a real time line segment detector with a false detection control
- Use of comment keyword in database
- 214. 最短回文串
- 72. 编辑距离
- Take you through a circuit board, from design to production (dry goods)
- 242. 有效的字母异位词
- [deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
- How do I use Google Chrome 11's Upload Folder feature in my own code?
猜你喜欢

Listener listener

Thread data sharing and security -threadlocal
![[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model](/img/05/85c004e4fbfc8d4984ac04ddb1190b.png)
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model

Home online shopping project

Cookie&Session

ASGNet论文和代码解读2

[TA frost wolf \u may - "hundred people plan"] 2.1 color space

Feature pyramid networks for object detection
![[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st

Gorilla/mux framework (RK boot): RPC error code design
随机推荐
静态库使用MFC和共享库使用MFC的区别
LeetCode 128最长连续序列(哈希set)
Thread data sharing and security -threadlocal
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
Sort linked list (merge sort)
166. 分数到小数
Develop industrial Internet with the technical advantages of small programs
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
Pyramid scene parsing network [pspnet] thesis reading
torch.histc
Nacos
Valentine's Day is nothing.
MFC窗口滚动条用法
【TA-霜狼_may-《百人计划》】2.1 色彩空间
完全背包问题
Future of NTF and trends in 2022
242. 有效的字母异位词
5. [WebGIS practice] software operation - service release and permission management
Feature pyramid networks for object detection
Database DDL (data definition language) knowledge points