import java.util.*; public class Solution { /** * * @param S string字符串 * @param T string字符串 * @return string字符串 */ public String minWindow (String S, String T) { // write code here if (S == null || T == null) { return ""; } char[] sArr = S.toCharArray(); char[] tArr = T.toCharArray(); int n = sArr.length; int m = tArr.length; if (n == 0 || m == 0) { return ""; } int[] countArr = new int[128]; for (char ch : tArr) { countArr[ch]--; } int slow = 0; int fast = 0; int min = Integer.MAX_VALUE; int left = -1; int right = -1; while (fast < n) { char ch = sArr[fast]; countArr[ch]++; while (check(countArr)) { if (fast - slow + 1 < min) { min = fast - slow + 1; left = slow; right = fast; } countArr[sArr[slow]]--; slow++; } fast++; } if (left == -1) { return ""; } return S.substring(left, right + 1); } private boolean check(int[] countArr) { for (int cnt : countArr) { if (cnt < 0) { return false; } } return true; } }