当前位置:网站首页>[30. concatenate substrings of all words]
[30. concatenate substrings of all words]
2022-06-23 17:19:00 【Sugar_ wolf】
source : Power button (LeetCode)
describe :
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]
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
Method : The sliding window
Ideas
remember words The length of is m, words The length of each word in is n,s The length of is ls. First, you need to s Divide into word groups , The size of each word is n ( Except the beginning and end ). Such division methods include n Kind of , I.e. delete the former i ( i = 0 ∼ n − 1) After two letters , Divide the remaining letters , If there is less than at the end n The letters are also deleted . For this n The resulting word arrays are divided into two groups by using sliding window pairs words Proceed similarly to 「 Letter heterotopic word 」 My search .
Divide into word groups , A window contains s Middle front m Word , With a hash table differ Indicates the word frequency and... In the window words The difference in the frequency of words in . initialization differ when , Words that appear in the window , Every time , The corresponding value increases 1, Appear in the words The words in , Every time , The corresponding value decreases 1. Then move the window to the right , A word will be added to the right , A word will be moved out on the left , Also on differ Update accordingly . When the window moves , If it appears differ The median 0 The number of keys of is 0, It means the word frequency and words The frequency of Chinese words is the same , The left endpoint of the window is a starting position to be found . The methods of division are n Kind of , do n After sliding the window a second time , You can find all the starting positions .
Code :
class Solution {
public:
vector<int> findSubstring(string &s, vector<string> &words) {
vector<int> res;
int m = words.size(), n = words[0].size(), ls = s.size();
for (int i = 0; i < n && i + m * n <= ls; ++i) {
unordered_map<string, int> differ;
for (int j = 0; j < m; ++j) {
++differ[s.substr(i + j * n, n)];
}
for (string &word: words) {
if (--differ[word] == 0) {
differ.erase(word);
}
}
for (int start = i; start < ls - m * n + 1; start += n) {
if (start != i) {
string word = s.substr(start + (m - 1) * n, n);
if (++differ[word] == 0) {
differ.erase(word);
}
word = s.substr(start - n, n);
if (--differ[word] == 0) {
differ.erase(word);
}
}
if (differ.empty()) {
res.emplace_back(start);
}
}
}
return res;
}
};
Execution time :56 ms, In all C++ Defeated in submission 72.93% Users of
Memory consumption :33.1 MB, In all C++ Defeated in submission 34.82% Users of
author:LeetCode-Solution
边栏推荐
- QT layout manager [qvboxlayout, qhboxlayout, qgridlayout]
- 面渣逆袭:MySQL六十六问!建议收藏
- 你女朋友也能读懂的LAMP架构
- OutputDebugString instructions and exception handling
- Apache foundation officially announced Apache inlong as a top-level project
- 右腿驱动电路原理?心电采集必备,有仿真文件!
- Another breakthrough! Alibaba cloud enters the Gartner cloud AI developer service Challenger quadrant
- ABAP essays - program optimization notes
- Practice sharing of chaos engineering in stability management of cloud native Middleware
- 什么是抽象类?怎样定义抽象类?
猜你喜欢

Digital twin excavator of Tupu software realizes remote control

How to use SQL window functions
![[go]沙盒环境下调用支付宝扫码支付](/img/d4/c6d72a697bc08f69f11121a15109b3.png)
[go]沙盒环境下调用支付宝扫码支付

【30. 串联所有单词的子串】

Here comes the official zero foundation introduction jetpack compose Chinese course!

官方零基础入门 Jetpack Compose 的中文课程来啦!

测试的重要性及目的

Redis cluster operation method

TensorRT Paser加载onnx 推理使用
![[network communication -- webrtc] source code analysis of webrtc -- bandwidth estimation at the receiving end](/img/b0/97dbf3d07a4ed86d6650a58a97a5fc.png)
[network communication -- webrtc] source code analysis of webrtc -- bandwidth estimation at the receiving end
随机推荐
ABAP essay - material master data interface enhancement
ASEMI快恢复二极管RS1M、US1M和US1G能相互代换吗
What does websocket do?
Date转换为LocalDateTime
Short video platform development, click the input box to automatically pop up the soft keyboard
C#与数据库连接
Codeforces Round #620 (Div. 2)ABC
开户券商怎么选择?现在网上开户安全么?
时间戳90K是什么意思?
Online communication - the combination of machine learning and knowledge reasoning in trusted machine learning (Qing Yuan talk, issue 20, Li Bo)
面渣逆袭:MySQL六十六问!建议收藏
How important is 5g dual card dual access?
[qsetting and.Ini configuration files] and [create resources.qrc] in QT
Jmeter压力测试教程
How to make sales management more efficient?
供求两端的对接将不再是依靠互联网时代的平台和中心来实现的
Right leg drive circuit principle? ECG acquisition is a must, with simulation files!
Lamp architecture that your girlfriend can read
【网络通信 -- WebRTC】WebRTC 源码分析 -- 接收端带宽估计
出现Identify and stop the process that‘s listening on port 8080 or configure this application等解决方法