当前位置:网站首页>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/
边栏推荐
- JS daily development tips (continuous update)
- The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
- Server rendering technology JSP
- Pyramid scene parsing network [pspnet] thesis reading
- Binary tree god level traversal: Morris traversal
- 【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍
- Feature Pyramid Networks for Object Detection论文理解
- 快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
- Take you through a circuit board, from design to production (dry goods)
- Implement pow (x, n) function
猜你喜欢

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
![[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
![[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions](/img/be/325f78dee744138a865c13d2c20475.png)
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions

实现pow(x,n)函数

复习专栏之---消息队列

使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况

【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹

Appium自动化测试基础 — APPium基本原理

完全背包问题

Idea plug-in backup table
随机推荐
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
8. string conversion integer (ATOI)
bootsrap中的栅格系统
Thread data sharing and security -threadlocal
165. compare version numbers
数据库DDL(Data Definition Language,数据定义语言)知识点
静态库使用MFC和共享库使用MFC的区别
TEC: Knowledge Graph Embedding with Triple Context
Download and installation configuration of cygwin
Pyramid Scene Parsing Network【PSPNet】论文阅读
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
165. 比较版本号
10、Scanner. Next() cannot read spaces /indexof -1
5. [WebGIS practice] software operation - service release and permission management
用小程序的技术优势发展产业互联网
Take you through a circuit board, from design to production (dry goods)
【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍
【TA-霜狼_may-《百人计划》】2.1 色彩空间
pytorch训练深度学习网络设置cuda指定的GPU可见
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表