当前位置:网站首页>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
边栏推荐
- ANR分析--问题1
- Risc-v instruction set
- UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation
- Stability summary
- Why does next() in iterator need to be forcibly converted?
- 怎么理解云原生数据库的易用性?
- Various types of long
- Troubleshooting of pyinstaller failed to pack pikepdf
- Characters and integers
- 题解 The SetStack Computer(UVa12096)紫书P116STL的综合应用
猜你喜欢

【学习笔记】主成分分析法介绍

Alibaba cloud MSE full link grayscale solution practice based on Apache apisik

Flatten of cnn-lstm

2022 welder (elementary) special operation certificate examination question bank and answers

CNN-LSTM的flatten

C # connect to the database to complete the operation of adding, deleting, modifying and querying
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

如何使用 DataAnt 监控 Apache APISIX

Are you still paying for your thesis? Come and join me

On the complexity of software development and the way to improve its efficiency
随机推荐
Are you still paying for your thesis? Come and join me
基于 Apache APISIX 的自动化运维平台
Rsync remote synchronization
2022 t elevator repair test question bank simulation test platform operation
LeetCode每日一题——324. 摆动排序 II
[learning notes] cluster analysis
How to use dataant to monitor Apache apisex
Tcwind mode setting
理解整个网络模型的构建
3. integrate listener
输入和输出实型数据
2. 整合 Filter
【学习笔记】因子分析
Relevant calculation of sphere, etc
应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)
Flatten of cnn-lstm
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
rapid ssl通配符证书八百一年是正版吗
Visualization of neural network structure in different frames