当前位置:网站首页>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
边栏推荐
- Use of WC command
- 如何做好客户成功的底层设计|ToB大师课
- 03.hello_rust
- How to open an account in great wisdom? Is it safe
- 图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
- 【学习笔记】主成分分析法介绍
- [go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences
- 券商公司开户哪个最靠谱最安全呢
- 数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
- 请允许当下国内ToB的「不完美」
猜你喜欢
![[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences

方 差 分 析

mysql-发生系统错误1067

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

Analysis of all knowledge points of TCP protocol in network planning

ref属性,props配置,mixin混入,插件,scoped样式

2022 P cylinder filling test exercises and online simulation test

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

with torch.no_grad():的使用原因

Leetcode 36. 有效的数独(可以,一次过)
随机推荐
Anr problem - camera related debug
如何使用 DataAnt 监控 Apache APISIX
2022 t elevator repair test question bank simulation test platform operation
Flatten of cnn-lstm
CNN-LSTM的flatten
Is the inter-bank certificate of deposit reliable and safe
如何添加 logs来debug ANR 问题
input separator
Automatic operation and maintenance platform based on Apache APIs
圆球等的相关计算
题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
Is it safe for CICC fortune to open an account? Let's talk about CICC fortune
【学习笔记】主成分分析法介绍
Use of WC command
List of domestic database directory
With a market value of $120billion, how did intuit, an old tax giant, do it?
On the complexity of software development and the way to improve its efficiency
GlobalSign的泛域名SSL证书
Troubleshooting of pyinstaller failed to pack pikepdf
阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践