当前位置:网站首页>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/
边栏推荐
- Feature Pyramid Networks for Object Detection论文理解
- How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
- Future of NTF and trends in 2022
- 171. Excel 表列序号
- Error: plug ins declaring extensions or extension points must set the singleton directive to true
- [reach out to Party welfare] developer reload system sequence
- 208. 实现 Trie (前缀树)
- 4. [WebGIS practice] software operation chapter - data import and processing
- 【伸手党福利】JSONObject转String保留空字段
- 168. Excel表列名称
猜你喜欢

Data exchange JSON

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

【TA-霜狼_may-《百人计划》】1.3纹理的秘密

Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)

【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍

Future of NTF and trends in 2022

报错:Plug-ins declaring extensions or extension points must set the singleton directive to true

IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?

pytorch nn. AdaptiveAvgPool2d(1)

【TA-霜狼_may-《百人计划》】2.1 色彩空间
随机推荐
Pathmeasure implements loading animation
【快捷键】
TEC: Knowledge Graph Embedding with Triple Context
Explain spark operation mode in detail (local+standalone+yarn)
jeecgboot输出日志,@Slf4j的使用方法
The difference between MFC for static libraries and MFC for shared libraries
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
Valid brackets (force deduction 20)
Cookie&Session
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
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
Design of serial port receiving data scheme
165. 比较版本号
392. 判断子序列
复习专栏之---消息队列
Learning notes for introduction to C language multithreaded programming
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
Golang multi graph generation gif
[reach out to Party welfare] developer reload system sequence
数据库DDL(Data Definition Language,数据定义语言)知识点