You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

package 03.mca_09;
// 给你一个字符串 s 、一个字符串 t
// 返回 s 中涵盖 t 所有字符的最小子串
// 如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。
// 注意:对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量
// 如果 s 中存在这样的子串,我们保证它是唯一的答案
// 测试链接 : https://leetcode.cn/problems/minimum-window-substring/
public class Code03_MinWindowLength {
public static String minWindow(String s, String t) {
if (s.length() < t.length()) {
return "";
}
char[] str = s.toCharArray();
char[] target = t.toCharArray();
int[] map = new int[256];
for (char cha : target) {
map[cha]++;
}
int all = target.length;
int L = 0;
int R = 0;
int minLen = Integer.MAX_VALUE;
int ansl = -1;
int ansr = -1;
while (R != str.length) {
map[str[R]]--;
if (map[str[R]] >= 0) {
all--;
}
if (all == 0) {
while (map[str[L]] < 0) {
map[str[L++]]++;
}
if (minLen > R - L + 1) {
minLen = R - L + 1;
ansl = L;
ansr = R;
}
all++;
map[str[L++]]++;
}
R++;
}
return minLen == Integer.MAX_VALUE ? "" : s.substring(ansl, ansr + 1);
}
}