# leecode76. 最小覆盖子串

## 76. 最小覆盖子串

输入：s = "ADOBECODEBANC", t = "ABC"



输入：s = "a", t = "a"



输入: s = "a", t = "aa"



class Solution {
public String minWindow(String s, String t) {
int sLength = s.length();
int tLength = t.length();
if(tLength>sLength||tLength==0||sLength==0){
return "";
}
char[] charArrayS = s.toCharArray();
char[] charArrayT = t.toCharArray();
char[] tFreq = new char[128];
char[] winFreq = new char[128];
for(char c : charArrayT){
tFreq[c]++;
}
int left = 0;
int right = 0;
int distance = 0;
int mLen = sLength+1;
int begin = 0;
while(right < sLength){
if(tFreq[charArrayS[right]] == 0){
right++;
continue;
}
if(winFreq[charArrayS[right]]<tFreq[charArrayS[right]]){
distance++;
}
winFreq[charArrayS[right]]++;
right++;
while(distance == tLength){
if(right-left<mLen){
mLen = right-left;
begin =left;
}
if(tFreq[charArrayS[left]] == 0){
left++;
continue;
}
if(winFreq[charArrayS[left]]==tFreq[charArrayS[left]]){
distance--;
}
winFreq[charArrayS[left]]--;
left++;
}
}
return mLen == sLength+1 ? "" : s.substring(begin,begin+mLen);
}
}

posted @ 2022-07-20 17:19  zqy123  阅读(27)  评论(0编辑  收藏  举报