1. 问题所有代码:Tian-hy/c_leetcode
给你一个字符串s、一个字符串t。返回s中涵盖t所有字符的最小子串。如果s中不存在涵盖t所有字符的子串,则返回空字符串""。
注意:
对于t中重复字符,我们寻找的子字符串中该字符数量必须不少于t中该字符数量。
如果s中存在这样的子串,我们保证它是唯一的答案。
示例 1:
输入:s = “ADOBECODEBANC”, t = “ABC”
输出:“BANC”
示例 2:
输入:s = “a”, t = “a”
输出:“a”
2. 解决思路示例 3:
输入: s = “a”, t = “aa”
输出: “”
解释: t 中两个字符 ‘a’ 均应包含在 s 的子串中,
因此没有符合条件的子字符串,返回空字符串。
int is_win_substring(int *freq_s, int *freq_t){
for (int i=0; i<'z'-'A'+1; i++){
if (freq_s[i] < freq_t[i]){
return 0;
}
}
return 1;
}
char * minWindow(char * s, char * t){
int i = 0, j = -1;
int len = strlen(s), res_len = strlen(s)+1;
int freq_s['z'-'A'+1] = {0};
int freq_t['z'-'A'+1] = {0};
int idx[2] = {0};
for (; i
freq_t[t[i]-'A']++;
}
i = 0;
while (i < len){
if (j+1 < len && !is_win_substring(freq_s, freq_t)){
freq_s[s[++j]-'A']++;
}
else{
freq_s[s[i++]-'A']--;
}
if (is_win_substring(freq_s, freq_t))
if (res_len > j-i+1){
res_len = j-i+1;
idx[0] = i;
idx[1] = j;
}
}
if (res_len == strlen(s)+1){
return "";
}
else{
char *res = (char *)malloc(sizeof(char)*(idx[1]-idx[0]+2));
for (int i=0; i
res[i] = s[i+idx[0]];
}
res[idx[1]-idx[0]+1] = ' ';
return res;
}
}



