当前位置:网站首页>【30. 串联所有单词的子串】
【30. 串联所有单词的子串】
2022-06-23 16:30:00 【Sugar_wolf】
来源:力扣(LeetCode)
描述:
给定一个字符串 s 和一些 长度相同 的单词 words 。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
注意子串要与 words 中的单词完全匹配,中间不能有其他字符 ,但不需要考虑 words 中单词串联的顺序。
示例 1:
输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。
示例 2:
输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]
示例 3:
输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
输出:[6,9,12]
提示:
- 1 <= s.length <= 104
- s 由小写英文字母组成
- 1 <= words.length <= 5000
- 1 <= words[i].length <= 30
- words[i] 由小写英文字母组成
方法 :滑动窗口
思路
记 words 的长度为 m, words 中每个单词的长度为 n,s 的长度为 ls。首先需要将 s 划分为单词组,每个单词的大小均为 n (首尾除外)。这样的划分方法有 n 种,即先删去前 i ( i = 0 ∼ n − 1)个字母后,将剩下的字母进行划分,如果末尾有不到 n 个字母也删去。对这 n 种划分得到的单词数组分别使用滑动窗口对 words 进行类似于「字母异位词」的搜寻。
划分成单词组后,一个窗口包含 s 中前 m 个单词,用一个哈希表 differ 表示窗口中单词频次和 words 中单词频次之差。初始化 differ 时,出现在窗口中的单词,每出现一次,相应的值增加 1,出现在 words 中的单词,每出现一次,相应的值减少 1。然后将窗口右移,右侧会加入一个单词,左侧会移出一个单词,并对 differ 做相应的更新。窗口移动时,若出现 differ 中值不为 0 的键的数量为 0,则表示这个窗口中的单词频次和 words 中单词频次相同,窗口的左端点是一个待求的起始位置。划分的方法有 n 种,做 n 次滑动窗口后,即可找到所有的起始位置。
代码:
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;
}
};
执行用时:56 ms, 在所有 C++ 提交中击败了72.93%的用户
内存消耗:33.1 MB, 在所有 C++ 提交中击败了34.82%的用户
author:LeetCode-Solution
边栏推荐
- Golang writes to JSON files
- QT布局管理器【QVBoxLayout,QHBoxLayout,QGridLayout】
- 【网络通信 -- WebRTC】WebRTC 源码分析 -- 接收端带宽估计
- ASEMI快恢复二极管RS1M、US1M和US1G能相互代换吗
- What is an abstract class? How to define abstract classes?
- 面渣逆袭:MySQL六十六问!建议收藏
- Amadis publishes Ola payment processing standards
- 什么是抽象类?怎样定义抽象类?
- 电感参数有哪些?怎么选择电感?
- 《MPLS和VP体系结构》
猜你喜欢

Tupu digital twin 3D wind farm, offshore wind power of smart wind power
![leetcode:30. Concatenate substrings of all words [counter matching + pruning]](/img/a2/91ccaec4cc3dab27c566184b74e561.png)
leetcode:30. Concatenate substrings of all words [counter matching + pruning]

Shushulang passed the listing hearing: the gross profit margin of the tablet business fell, and the profit in 2021 fell by 11% year-on-year

Identify and stop the process that's listening on port 8080 or configure this application

Tensorrt Paser loading onnx inference use

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

DataNode进入Stale状态问题排查

Tupu software builds smart city with lightweight modeling

QT当中的【QSetting和.ini配置文件】以及【创建Resources.qrc】

ASEMI快恢复二极管RS1M、US1M和US1G能相互代换吗
随机推荐
Reading and writing JSON files by golang
OutputDebugString instructions and exception handling
ADC数字地DGND、模拟地AGND的谜团!
What is an abstract class? How to define abstract classes?
leetcode:30. Concatenate substrings of all words [counter matching + pruning]
Lamp architecture that your girlfriend can read
EasyPlayer移动端播放webrtc协议时长按播放页面无法关闭“关于我们”页面
Code examples of golang goroutine, channel and time
你的PCB地线走的对吗?为什么要有主地?
Innovative technology leader! Huawei cloud gaussdb won the 2022 authoritative award in the field of cloud native database
供求两端的对接将不再是依靠互联网时代的平台和中心来实现的
科大讯飞神经影像疾病预测方案!
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)
Tensorrt Paser loading onnx inference use
Network remote access raspberry pie (VNC viewer)
Implementation of golang bubble sort code
ABAP随笔-程序优化笔记
TensorRT Paser加载onnx 推理使用
bypassuac提权
Google Play Academy 组队 PK 赛,火热进行中!
