当前位置:网站首页>[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
边栏推荐
- 使用Jmeter进行性能测试及性能监控平台搭建
- 图扑软件数字孪生挖掘机实现远程操控
- ABP framework - data access infrastructure (Part 2)
- Jmeter压力测试教程
- The connection between supply and demand will no longer depend on the platform and center of the Internet Era
- Asemi ultrafast recovery diode es1j parameters, es1j package, es1j specification
- How to select an oscilloscope? These 10 points must be considered!
- The company recruited a tester with five years' experience and saw the real test ceiling
- Talk about the difference between redis cache penetration and cache breakdown, and the avalanche effect caused by them
- Case analysis of camera power supply disturbed, seriously affecting image quality
猜你喜欢

NPM install problem solving (NVM installation and use)

Huawei mobile phones install APK through ADB and prompt "the signature is inconsistent. The application may have been modified."

How important is 5g dual card dual access?

What does the timestamp 90K mean?
![[qsetting and.Ini configuration files] and [create resources.qrc] in QT](/img/67/85a5e7f6ad4220600acd377248ef46.png)
[qsetting and.Ini configuration files] and [create resources.qrc] in QT

Network remote access raspberry pie (VNC viewer)

Focus: zk-snark Technology

Robot Orientation and some misunderstandings in major selection in college entrance examination

美团三面:聊聊你理解的Redis主从复制原理?

出现Identify and stop the process that‘s listening on port 8080 or configure this application等解决方法
随机推荐
What does the timestamp 90K mean?
Elk log collection system deployment
How do you choose to buy stocks? Good security?
如何选择券商?手机开户安全么?
How important is 5g dual card dual access?
Bypass rights
右腿驱动电路原理?心电采集必备,有仿真文件!
Three minutes to learn how to retrieve the MySQL password
Easyplayer mobile terminal plays webrtc protocol for a long time. Pressing the play page cannot close the "about us" page
开户券商怎么选择?现在网上开户安全么?
OutputDebugString instructions and exception handling
The evolution of social structure and capital system brought about by the yuan universe
接口的所有权之争
Get first and last days by year
内网渗透令牌窃取
QT布局管理器【QVBoxLayout,QHBoxLayout,QGridLayout】
Jmeter压力测试教程
Redis cluster operation method
Six stone programming: the subtlety of application
Intranet penetration token stealing
