1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| class Solution { public String minWindow(String s, String t) { int ansLeft = 0, ansRight = Integer.MAX_VALUE; int[] cnt = new int[128]; for (char ch : t.toCharArray()) { cnt[ch]++; } int left = 0; for (int right = 0; right < s.length(); right++) { char ch = s.charAt(right); cnt[ch]--; while (check(cnt)) { if (ansRight - ansLeft > right - left) { ansLeft = left; ansRight = right; } cnt[s.charAt(left)]++; left++; } } return ansRight != Integer.MAX_VALUE ? s.substring(ansLeft, ansRight + 1) : ""; }
private boolean check(int[] cnt) { for (int c : cnt) { if (c > 0) return false; } return true; } }
|