当前位置:网站首页>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/
边栏推荐
- How do I use Google Chrome 11's Upload Folder feature in my own code?
- Learning notes for introduction to C language multithreaded programming
- Ouc2021 autumn - Software Engineering - end of term (recall version)
- 8. 字符串转换整数 (atoi)
- Server rendering technology JSP
- 【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
- The combination of applet container technology and IOT
- Pyramid scene parsing network [pspnet] thesis reading
- 389. 找不同
- Review column - message queue
猜你喜欢
用小程序的技术优势发展产业互联网
Cookie&Session
排序链表(归并排序)
Online public network security case nanny level tutorial [reaching out for Party welfare]
Idea plug-in backup table
SEM of C language_ Tvariable type
Download and installation configuration of cygwin
C语言的sem_t变量类型
The combination of applet container technology and IOT
数据库中COMMENT关键字的使用
随机推荐
Explain spark operation mode in detail (local+standalone+yarn)
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
【TA-霜狼_may-《百人计划》】2.1 色彩空间
[nine day training] content III of the problem solution of leetcode question brushing Report
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
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
Appium自动化测试基础--补充:C/S架构和B/S架构说明
Test function in pychram
【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
网页不能右键 F12 查看源代码解决方案
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
5. [WebGIS practice] software operation - service release and permission management
ECMAScript 6.0
Unexpected token o in JSON at position 1 ,JSON解析问题
Golang multi graph generation gif
171. Excel 表列序号
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
30. Concatenate substrings of all words
在 C 中声明函数之前调用函数会发生什么?