当前位置:网站首页>[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
边栏推荐
- ERP管理系统的重要性
- 官方零基础入门 Jetpack Compose 的中文课程来啦
- 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
- Intranet penetration token stealing
- How long does it take to open a stock account by mobile phone? Is online account opening safe?
- Apache基金会正式宣布Apache InLong成为顶级项目
- 创新技术领航者!华为云GaussDB获颁2022年云原生数据库领域权威奖项
- leetcode:面試題 08.13. 堆箱子【自頂而下的dfs + memory or 自底而上的排序 + dp】
- 10分钟后性能测试瓶颈调优!想进大厂这个必须会
- 记录——kubeadm集群node节点加入
猜你喜欢

【网络通信 -- WebRTC】WebRTC 源码分析 -- 接收端带宽估计

ctfshow php的特性

Troubleshooting of datanode entering stale status

I successfully joined the company with 27K ByteDance. This interview notes on software testing has benefited me for life

The evolution of social structure and capital system brought about by the yuan universe

What can the accelerated implementation of digital economy bring to SMEs?

华为手机通过adb安装APK提示“签名不一致,该应用可能已被修改”

C # connection to database

NPM install problem solving (NVM installation and use)

Implementation of golang bubble sort code
随机推荐
QT layout manager [qvboxlayout, qhboxlayout, qgridlayout]
Practice sharing of chaos engineering in stability management of cloud native Middleware
Why do we say that the data service API is the standard configuration of the data midrange?
供求两端的对接将不再是依靠互联网时代的平台和中心来实现的
Jetpack compose and material you FAQs
Get first and last days by year
Case analysis of camera power supply disturbed, seriously affecting image quality
Elk log collection system deployment
美团三面:聊聊你理解的Redis主从复制原理?
[untitled] Application of laser welding in medical treatment
Database Experiment 2 query
ASEMI超快恢复二极管ES1J参数,ES1J封装,ES1J规格
记录——kubeadm集群node节点加入
ABAP随笔-程序优化笔记
QT布局管理器【QVBoxLayout,QHBoxLayout,QGridLayout】
ABAP essay - material master data interface enhancement
Asemi ultrafast recovery diode es1j parameters, es1j package, es1j specification
Jetpack Compose 与 Material You 常见问题解答
C. Product 1 Modulo N-Codeforces Round #716 (Div. 2)
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
