当前位置:网站首页>【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
边栏推荐
- 以 27K 成功入职字节跳动,这份《 软件测试面试笔记》让我受益终身
- 2022 Jiufeng primary school (Optics Valley No. 21 primary school) student source survey
- JS common error reporting and exception capture
- Code implementation of golang binary search method
- 记录——kubeadm集群node节点加入
- Date转换为LocalDateTime
- Another breakthrough! Alibaba cloud enters the Gartner cloud AI developer service Challenger quadrant
- Golang writes to JSON files
- Is your PCB ground wire right? Why should we have a master land?
- ASEMI快恢复二极管RS1M、US1M和US1G能相互代换吗
猜你喜欢

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

《MPLS和VP体系结构》

Online communication - the combination of machine learning and knowledge reasoning in trusted machine learning (Qing Yuan talk, issue 20, Li Bo)

Stick to five things to get you out of your confusion

查数据库中每张表的大小

QT布局管理器【QVBoxLayout,QHBoxLayout,QGridLayout】

NPM install problem solving (NVM installation and use)

How to make sales management more efficient?

Interface ownership dispute

Golang writes to JSON files
随机推荐
How to make sales management more efficient?
Intel arc A380 graphics card message summary: the entry-level price products of running point and bright driving need to be optimized
Code examples of golang goroutine, channel and time
Redis cluster operation method
Here comes the official zero foundation introduction jetpack compose Chinese course!
Apache foundation officially announced Apache inlong as a top-level project
数学分析_证明_第1章:可数个可数集之并为可数集
Identify and stop the process that's listening on port 8080 or configure this application
右腿驱动电路原理?心电采集必备,有仿真文件!
NPM install problem solving (NVM installation and use)
Mobile cloud jointly builds the capability base of information innovation cloud and helps the development of China's information innovation industry
Implementation of golang bubble sort code
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
Image saving: torchvision utils. save_ image(img, imgPath)
How can the points mall make profits
Counter attack by flour dregs: MySQL 66 question! Suggested collection
TensorRT Paser加载onnx 推理使用
How long does it take to open a stock account by mobile phone? Is online account opening safe?
Taishan Office Technology Lecture: four cases of using Italic Font
leetcode:面試題 08.13. 堆箱子【自頂而下的dfs + memory or 自底而上的排序 + dp】
