-
滑动窗口的思想:
- 使用两个指针
left
和right
来表示当前窗口的边界,初始时它们都指向字符串s
的开头。 - 通过移动
right
指针扩大窗口,直到窗口包含了t
中所有的字符。 - 然后尝试移动
left
指针收缩窗口,使得窗口仍然包含t
中所有的字符,并记录最小的窗口。 - 继续移动
right
,并不断调整left
,直到遍历完所有字符
- 使用两个指针
leetcode:
给定一个字符串 s
和一个字符串数组 words
。 words
中所有字符串 长度相同。
s
中的 串联子串 是指一个包含 words
中所有字符串以任意顺序排列连接起来的子串。
- 例如,如果
words = ["ab","cd","ef"]
, 那么"abcdef"
,"abefcd"
,"cdabef"
,"cdefab"
,"efabcd"
, 和"efcdab"
都是串联子串。"acdbef"
不是串联子串,因为他不是任何words
排列的连接。
返回所有串联子串在 s
中的开始索引。你可以以 任意顺序 返回答案。
示例 1:
输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:因为 words.length == 2 同时 words[i].length == 3,连接的子字符串的长度必须为 6。
子串 "barfoo" 开始位置是 0。它是 words 中以 ["bar","foo"] 顺序排列的连接。
子串 "foobar" 开始位置是 9。它是 words 中以 ["foo","bar"] 顺序排列的连接。
输出顺序无关紧要。返回 [9,0] 也是可以的。
示例 2:
输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]
解释:因为 words.length == 4 并且 words[i].length == 4,所以串联子串的长度必须为 16。
s 中没有子串长度为 16 并且等于 words 的任何顺序排列的连接。
所以我们返回一个空数组。
示例 3:
输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"] 输出:[6,9,12] 解释:因为 words.length == 3 并且 words[i].length == 3,所以串联子串的长度必须为 9。 子串 "foobarthe" 开始位置是 6。它是 words 中以 ["foo","bar","the"] 顺序排列的连接。 子串 "barthefoo" 开始位置是 9。它是 words 中以 ["bar","the","foo"] 顺序排列的连接。 子串 "thefoobar" 开始位置是 12。它是 words 中以 ["the","foo","bar"] 顺序排列的连接。
题解:
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res;
int m=words.size(),n=words[0].size(),l=s.length();
for(int i=0;i<n&&l>=i+m*n;i++){
unordered_set<string,int> d;
for(int j=0;j<m;j++){
++d[s.substr(i+j*n,n)];
}
for(string &word:words){
if(--d[word]==0){
d.erase(word);
}
}
//滑动窗口
// 遍历 words 中的每个单词,检查其在 differ 中的出现次数
// 内层循环,从起始位置 i 开始滑动窗口
for (int start = i; start < ls - m * n + 1; start += n) {
if (start != i) {
// 添加新进入窗口的单词到 differ 中
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);
}
}
// 如果 differ 为空,表示当前窗口符合要求,将起始位置加入结果数组 res
if (differ.empty()) {
res.emplace_back(start);
}
}
}
return res; // 返回所有符合要求的起始位置数组
}
}
};
提示
给你一个字符串 s
、一个字符串 t
。返回 s
中涵盖 t
所有字符的最小子串。如果 s
中不存在涵盖 t
所有字符的子串,则返回空字符串 ""
。
注意:
- 对于
t
中重复字符,我们寻找的子字符串中该字符数量必须不少于t
中该字符数量。 - 如果
s
中存在这样的子串,我们保证它是唯一的答案。
示例 1:
输入:s = "ADOBECODEBANC", t = "ABC" 输出:"BANC" 解释:最小覆盖子串 "BANC" 包含来自字符串 t 的 'A'、'B' 和 'C'。
示例 2:
输入:s = "a", t = "a" 输出:"a" 解释:整个字符串 s 是最小覆盖子串。
示例 3:
输入: s = "a", t = "aa" 输出: "" 解释: t 中两个字符 'a' 均应包含在 s 的子串中, 因此没有符合条件的子字符串,返回空字符串。
题解:
class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char, int> targetCount; // 记录 t 中每个字符的频率
unordered_map<char, int> windowCount; // 记录当前窗口内字符的频率
// 统计 t 中每个字符的出现次数
for (char c : t) {
targetCount[c]++;
}
int left = 0, right = 0; // 滑动窗口的左右指针
int minLength = INT_MAX; // 最小子串的长度
int start = 0; // 最小子串的起始位置
int required = targetCount.size(); // t 中需要的不同字符数
int formed = 0; // 当前窗口中已经满足要求的字符种类数
while(right<s.size()){
char c=s[right];
windowCount[c]++;
if(targetCount.count(c)>0&&targetCount[c]==windowCount[c]){
formed++;
}
while(left<=right&&formed==required){
// 更新最小窗口
// minLength=min(minLength,r-l+1);
if(minLength>right-left+1){
minLength=right-left+1;
start=left;
}
char c2=s[left];
windowCount[c2]--;
if(targetCount.count(c2)>0&&targetCount[c2]>windowCount[c2]){
formed--;
}
left++;
}
right++;
}
// 如果 minLength 没有更新过,说明没有找到符合条件的子串
if (minLength == INT_MAX) {
return "";
}
// 返回最小窗口的子串
return s.substr(start, minLength);
}
};