当前位置:网站首页>30. Concatenate substrings of all words
30. Concatenate substrings of all words
2022-07-01 03:42:00 【Sun_ Sky_ Sea】
30. Concatenate the substrings of all words
Original title link :https://leetcode.cn/problems/substring-with-concatenation-of-all-words/
Given a string s And some of the Same length 's words words . find s It happens that words The starting position of the substring formed by concatenation of all words in .
Pay attention to the relationship between the string and words The words in match exactly , No other characters in the middle , But there's no need to think about words The order in which words are concatenated .
Example 1:
Input :s = “barfoothefoobarman”, words = [“foo”,“bar”]
Output :[0,9]
explain :
From the index 0 and 9 The first substrings are “barfoo” and “foobar” .
The order of output is not important , [9,0] It's also a valid answer .
Example 2:
Input :s = “wordgoodgoodgoodbestword”, words = [“word”,“good”,“best”,“word”]
Output :[]
Example 3:
Input :s = “barfoofoobarthefoobarman”, words = [“bar”,“foo”,“the”]
Output :[6,9,12]
Their thinking :
stay s Start searching with the window size words Whether all the words of are included , Is continuous and used up .
Code implementation :
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
# Statistics words The words and their numbers in
words_dict = {
}
for word in words:
words_dict[word] = words_dict.get(word, 0) + 1
# words The length of the word , The explanation is that each word is the same length
word_len = len(words[0])
# The length of the window : The number of words * Word length
windows_len = len(words) * word_len
# Given string length
s_len = len(s)
ans = []
# In the size of the window , In string s Search for ,i Is the starting point of each window
for i in range(s_len - windows_len + 1):
# Every time you use a statistical Dictionary , And you need to reduce the number to verify whether it meets the requirements of the topic
# So use words_dict Shallow copy , Recycling , The equal sign refers to the same memory address
# deepcopy It's a deep copy , Parent independent , Child objects do not change , And you need to import import copy
temp_dict = words_dict.copy()
# j It is the end of a word in each window
j = i + word_len
# If s Found the string in words One of the words in , And the count in the dictionary is greater than 0 Words ( It means it hasn't been used up )
while s[j - word_len : j] in temp_dict and temp_dict[s[j - word_len : j]] > 0:
# Count of currently used words minus 1
temp_dict[s[j - word_len : j]] -= 1
# Start next word position
j += word_len
# If the i Is the starting point and the words in the window are words_dict in , Then it meets the conditions
if sum(temp_dict.values()) == 0:
ans.append(i)
return ans
reference :
https://leetcode.cn/problems/substring-with-concatenation-of-all-words/solution/san-chong-fang-fa-zhu-jian-you-hua-by-hardcandy/
边栏推荐
- JS daily development tips (continuous update)
- Download and installation configuration of cygwin
- The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
- 使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
- 完全背包问题
- 409. 最长回文串
- multiple linear regression
- 在线公网安备案保姆级教程【伸手党福利】
- Use of comment keyword in database
- Sort linked list (merge sort)
猜你喜欢

Edge drawing: a combined real-time edge and segment detector

Idea plug-in backup table

Test function in pychram

pytorch训练深度学习网络设置cuda指定的GPU可见

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

Pytorch training deep learning network settings CUDA specified GPU visible

jeecgboot输出日志,@Slf4j的使用方法

pytorch nn. AdaptiveAvgPool2d(1)

How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars

完全背包问题
随机推荐
静态库使用MFC和共享库使用MFC的区别
10. 正则表达式匹配
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
5. [WebGIS practice] software operation - service release and permission management
【TA-霜狼_may-《百人计划》】1.3纹理的秘密
Database DDL (data definition language) knowledge points
Cookie&Session
小程序容器技术与物联网IoT的结合点
TEC: Knowledge Graph Embedding with Triple Context
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
187. 重复的DNA序列
【TA-霜狼_may-《百人计划》】2.1 色彩空间
6. Z 字形变换
谷粒学院微信扫码登录过程记录以及bug解决
网页不能右键 F12 查看源代码解决方案
LeetCode 128最长连续序列(哈希set)
The shell script uses two bars to receive external parameters
214. 最短回文串