forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
73 lines (63 loc) · 2.2 KB
/
Solution.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution {
public String minWindow(String s, String t) {
Map<Character, Integer> mp = new HashMap<>();
int begin = 0, end = 0, counter = t.length(), minLen = Integer.MAX_VALUE, minStart = 0, size = s.length();
for (char c : s.toCharArray())
mp.put(c, 0);
for (char c : t.toCharArray()) {
if (mp.containsKey(c))
mp.put(c, mp.get(c) + 1);
else
return "";
}
while (end < size) {
if (mp.get(s.charAt(end)) > 0)
counter--;
mp.put(s.charAt(end), mp.get(s.charAt(end)) - 1);
end++;
while (counter == 0) {
if (end - begin < minLen) {
minStart = begin;
minLen = end - begin;
}
mp.put(s.charAt(begin), mp.get(s.charAt(begin)) + 1);
if (mp.get(s.charAt(begin)) > 0) {
counter++;
}
begin++;
}
}
if (minLen != Integer.MAX_VALUE) {
return s.substring(minStart, minStart + minLen);
}
return "";
}
}
// class Solution {
// public String minWindow(String s, String t) {
// int[] count = new int['z' - 'A' + 1];
// int uniq = 0;
// for (int i = 0; i < t.length(); ++i) {
// if (++count[t.charAt(i) - 'A'] == 1) uniq++;
// }
// int found = 0,i = 0,j = 0;
// int minLen = Integer.MAX_VALUE;
// int minJ = Integer.MAX_VALUE;
// while (found < uniq) {
// while (i < s.length()) {
// if (found >= uniq) break;
// if (--count[s.charAt(i) - 'A'] == 0) found++;
// i++;
// }
// if (found < uniq) break;
// while (j < i && count[s.charAt(j) - 'A'] < 0) count[s.charAt(j++) - 'A']++;
// if (i - j < minLen) {
// minLen = i - j;
// minJ = j;
// }
// count[s.charAt(j++) - 'A']++;
// found--;
// }
// return minLen < Integer.MAX_VALUE ? s.substring(minJ, minJ + minLen) : "";
// }
// }