当前位置:网站首页>[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
边栏推荐
- Case analysis of camera power supply disturbed, seriously affecting image quality
- What does websocket do?
- 《MPLS和VP体系结构》
- Online communication - the combination of machine learning and knowledge reasoning in trusted machine learning (Qing Yuan talk, issue 20, Li Bo)
- 谈谈redis缓存击穿透和缓存击穿的区别,以及它们所引起的雪崩效应
- ABAP essay - material master data interface enhancement
- 什么是抽象类?怎样定义抽象类?
- ASEMI快恢复二极管RS1M、US1M和US1G能相互代换吗
- 10分钟后性能测试瓶颈调优!想进大厂这个必须会
- ASEMI超快恢复二极管ES1J参数,ES1J封装,ES1J规格
猜你喜欢

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

Troubleshooting of datanode entering stale status

Redis cluster operation method

Rongyun: let the bank go to the "cloud" easily

使用Jmeter进行性能测试及性能监控平台搭建

Intranet penetration token stealing
![[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
![[untitled] Application of laser welding in medical treatment](/img/c5/9c9edf1c931dfdd995570fa20cf7fd.png)
[untitled] Application of laser welding in medical treatment

Intel arc A380 graphics card message summary: the entry-level price products of running point and bright driving need to be optimized

图扑软件以轻量化建模构建智慧城市
随机推荐
移动云共筑信创云能力底座,助力中国信创产业发展
R language uses colorblinr package to simulate color blind vision, and uses edit to visualize the image of ggplot2_ The colors function is used to edit and convert color blindness into visual results
三分钟学会如何找回mysql密码
使用Jmeter进行性能测试及性能监控平台搭建
Interface ownership dispute
Elk log collection system deployment
The R language uses the GT package and the gtextras package to display tabular data gracefully and beautifully: gt of the gtextras package_ The sparkline function visualizes the line plot of the group
How can the points mall make profits
Jetpack compose and material you FAQs
短视频平台开发,点击输入框时自动弹出软键盘
A number of individual stocks in Hong Kong stocks performed actively, triggering investors' speculation and concern about the recovery of the Hong Kong stock market
JS常见的报错及异常捕获
Three minutes to learn how to retrieve the MySQL password
IFLYTEK neuroimaging disease prediction program!
What does the timestamp 90K mean?
Bypass rights
图扑软件以轻量化建模构建智慧城市
图扑软件数字孪生挖掘机实现远程操控
The evolution of social structure and capital system brought about by the yuan universe
How do you choose to buy stocks? Good security?
