当前位置:网站首页>Leetcode daily question - 30 Concatenate substrings of all words
Leetcode daily question - 30 Concatenate substrings of all words
2022-06-28 20:36:00 【Did HYK write the algorithm today】
List of articles
subject
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
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]
Tips :
1 <= s.length <= 104
s It's made up of lowercase letters
1 <= words.length <= 5000
1 <= words[i].length <= 30
words[i] It's made up of lowercase letters
Ideas
This question means to find a given s Middle energy quilt words All characters in the string can be used once to form the starting subscript of the substring .
because words The length of Chinese characters is consistent , So I thought I could use sliding windows , The window length is words The length of Chinese characters n.
And you need a hash table record words Characters in and the number of occurrences , because words Characters in may be duplicated , You can't simply use an array to determine whether it exists .
The way to do it is , Maintain this sliding window , Determine whether the value in the window is words in
- If not , Direct left and right ends at the same time +1, The whole window moves one unit to the right .
- If in , The number of corresponding characters in the hash table -1, Slide the window once to the right n A unit of , Directly determine whether the characters in the next window are in the hash table , If step above the loop , Be careful : The most important thing to judge here is words The length of the characters in , Because the title requires all characters to appear and appear once
Finally, judge whether all the values in the hash table are zero , If all are zero, record the left pointer of the sliding window , If it is not zero, the left and right ends shall be at the same time +1, The whole window moves to the right , Just repeat the above steps .
Answer key
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
n = len(words[0])
# Slide the pointers on both sides of the window
l, r = 0, n
res = []
temp = {}
# Hash table records characters and quantity
for i in words:
temp[i] = temp.get(i, 0) + 1
while r <= len(s):
ans = copy.copy(temp)
# The characters in the window exist in words
if s[l:r] in words:
# Each move n Unit judgment
for i in range(0, len(words)):
index = s[l+i*n:r+i*n]
if ans.get(index, 0) != 0:
ans[index] -= 1
else:
break
if len(set(ans.values())) == 1 and list(ans.values())[0]==0:
res.append(l)
# The whole window moves to the right
l += 1
r += 1
return res
边栏推荐
- 稳定性总结
- Is the inter-bank certificate of deposit reliable and safe
- Is it safe for CICC fortune to open an account? Let's talk about CICC fortune
- [learning notes] cluster analysis
- 数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
- 数据标准化处理
- Resilience4j retry source code analysis and retry index collection
- Troubleshooting of pyinstaller failed to pack pikepdf
- Analysis of variance
- 请允许当下国内ToB的「不完美」
猜你喜欢

CNN-LSTM的flatten

On the complexity of software development and the way to improve its efficiency

【毕业季·进击的技术er】努力只能及格,拼命才能优秀!

应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)

题解 Pie(POJ3122)超详细易懂的二分入门

Analysis of all knowledge points of TCP protocol in network planning

Lucene构建索引的原理及源代码分析

ThreadLocal principle

ThreadLocal原理

2022 tea master (intermediate) examination simulated 100 questions and simulated examination
随机推荐
新形势下的SaaS销售升级|ToB大师课
1. integrate servlets
ANR分析--问题1
CNN-LSTM的flatten
How do I download videos? Look at the super simple method!
Rsync remote synchronization
oracle delete误删除表数据后如何恢复
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
题解 Ananagrams(UVa156)紫书P113map的应用
阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
实型数运算
Lecture 30 linear algebra Lecture 4 linear equations
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
LeetCode每日一题——522. 最长特殊序列 II
应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
Input and output real data
SaaS sales upgrade under the new situation | tob Master Course
LeetCode每日一题——710. 黑名单中的随机数
2022 P cylinder filling test exercises and online simulation test