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
| class Solution { public String minWindow(String s, String t) { int n = s.length(), total = 0; int[] c1 = new int[60], c2 = new int[60]; for (char x : t.toCharArray()) { if (++c1[getIndex(x)] == 1) ++total; } String ans = ""; for (int i = 0, j = 0; j < n; ++j) { int idx1 = getIndex(s.charAt(j)); if (++c2[idx1] == c1[idx1]) --total; while (i < j) { int idx2 = getIndex(s.charAt(i)); if (c2[idx2] > c1[idx2] && --c2[idx2] >= 0) ++i; else break; } if (total == 0 && (ans.length() == 0 || ans.length() > j - i + 1)) ans = s.substring(i, j + 1); } return ans; }
private int getIndex(char x) { return x >= 'A' && x <= 'Z' ? x - 'A' + 26 : x - 'a'; } }
|