1. 对应力扣题目连接
2. 实现案例代码
public class MinimumCoveringSubstring {public static void main(String[] args) {System.out.println(minWindow("ADOBECODEBANC", "ABC")); System.out.println(minWindow("a", "a")); System.out.println(minWindow("a", "aa")); System.out.println(minWindow("AAABBB", "AB")); System.out.println(minWindow("ADOBECODEBANC", "XYZ")); System.out.println(minWindow("ABCDABCD", "BCD")); System.out.println(minWindow("ABCD", "AB")); System.out.println(minWindow("XYZABC", "ABC")); System.out.println(minWindow("AXBYCZ", "ABC")); System.out.println(minWindow("AAABBBCCC", "ABC")); }public static String minWindow(String s, String t) {if (s.length() == 0 || t.length() == 0) {return "";}Map<Character, Integer> need = new HashMap<>();for (char c : t.toCharArray()) {need.put(c, need.getOrDefault(c, 0) + 1);}Map<Character, Integer> window = new HashMap<>();int left = 0, right = 0;int valid = 0;int start = 0, minLength = Integer.MAX_VALUE;while (right < s.length()) {char c = s.charAt(right);right++;if (need.containsKey(c)) {window.put(c, window.getOrDefault(c, 0) + 1);if (window.get(c).intValue() == need.get(c).intValue()) {valid++;}}while (valid == need.size()) {if (right - left < minLength) {start = left;minLength = right - left;}char d = s.charAt(left);left++;if (need.containsKey(d)) {if (window.get(d).intValue() == need.get(d).intValue()) {valid--;}window.put(d, window.get(d) - 1);}}}return minLength == Integer.MAX_VALUE ? "" : s.substring(start, start + minLength);}
}