当前位置:网站首页>【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
边栏推荐
- 创新技术领航者!华为云GaussDB获颁2022年云原生数据库领域权威奖项
- Openresty Foundation
- Golang write file code example
- Tupu digital twin 3D wind farm, offshore wind power of smart wind power
- 元宇宙带来的社会结构和资本制度演变
- Is it cost-effective to buy a long-term financial product?
- Innovative technology leader! Huawei cloud gaussdb won the 2022 authoritative award in the field of cloud native database
- Here comes the official zero foundation introduction jetpack compose Chinese course!
- Golang writes to JSON files
- Stick to five things to get you out of your confusion
猜你喜欢

Comparison of asemi Schottky diode and ultrafast recovery diode in switching power supply

图扑数字孪生 3D 风电场,智慧风电之海上风电

数字经济加速落地,能为中小企业带来什么?

Mathematical analysis_ Certification_ Chapter 1: the union of countable sets is countable

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

Network remote access raspberry pie (VNC viewer)

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

The Google play academy team PK competition is in full swing!

The company recruited a tester with five years' experience and saw the real test ceiling

Jetpack compose and material you FAQs
随机推荐
三分钟学会如何找回mysql密码
leetcode:面試題 08.13. 堆箱子【自頂而下的dfs + memory or 自底而上的排序 + dp】
Openresty Foundation
面渣逆袭:MySQL六十六问!建议收藏
走好数据中台最后一公里,为什么说数据服务 API 是数据中台的标配?
TQ of R language using tidyquant package_ The transmute function calculates the daily, monthly and weekly returns of a stock. Ggplot2 uses the bar plot to visualize the monthly return data of the stoc
Asemi ultrafast recovery diode es1j parameters, es1j package, es1j specification
Golang writes to JSON files
如何选择券商?手机开户安全么?
stylegan1: a style-based henerator architecture for gemerative adversarial networks
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
Implementation of network data transmission by golang Gob
Focus: zk-snark Technology
VGg download (.Net file and imagenet-vgg-verydeep-19)
Jmeter压力测试教程
开户券商怎么选择?现在网上开户安全么?
How to configure MySQL log management
I successfully joined the company with 27K ByteDance. This interview notes on software testing has benefited me for life
Is your PCB ground wire right? Why should we have a master land?
Apache基金会正式宣布Apache InLong成为顶级项目
