就题目里那点描述,根本说不清需求,完全是一个个case调出来的。。。
bool isWindow(array<int,256>& flags, array<int,256>& counter){
for (int i = 0; i < 256; i++){
if (flags[i]!=0 && counter[i]<flags[i]){
return false;
}
}
return true;
}
void increaseCounter(array<int, 256>& flags, array<int, 256>& counter,char c){
if (flags[c]){
counter[c]++;
}
}
void decreaseCounter(array<int, 256>& flags, array<int, 256>& counter,char c){
if (flags[c]){
counter[c]--;
}
}
string minWindow(string S, string T) {
static array<int, 256> counter;
static array<int, 256> flags;
if (S.length()<T.length()){
return "";
}
counter.fill(0);
flags.fill(0);
for (auto& c:T){
flags[c]++;
}
int b = 0;
int e = 0;
int resb = 0;
int rese = S.length()-1;
bool hasResult = false;
for (int i = 0; i < S.length();++i){
increaseCounter(flags,counter,S[i]);
if (i<T.length()-1||!isWindow(flags,counter)){
continue;
}
hasResult = true;
e = i;
while (1){
if (flags[S[b]]==0){
b++;
continue;
}
decreaseCounter(flags,counter,S[b]);
if (counter[S[b]]<flags[S[b]]){
break;
}
b++;
}
if ((e-b)<(rese-resb)){
rese = e;
resb = b;
}
b++;
}
return hasResult?S.substr(resb,rese-resb+1):"";
}
寻找字符串中最短匹配窗口

本文探讨了在给定字符串S中找到包含另一个字符串T的所有字符的最短连续子串的方法,通过使用滑动窗口技术和计数器实现优化。
320

被折叠的 条评论
为什么被折叠?



