-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq076_MinimumWindowSubstring.java
More file actions
47 lines (40 loc) · 1.46 KB
/
q076_MinimumWindowSubstring.java
File metadata and controls
47 lines (40 loc) · 1.46 KB
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
package leetcode_algorithm;
/**
* Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
* For example,
* <p>
* S = "ADOBECODEBANC"
* T = "ABC"
* <p>
* Minimum window is "BANC".
* Note:
* If there is no such window in S that covers all characters in T, return the empty string "".
* If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
*/
public class q076_MinimumWindowSubstring {
public static void main(String[] args) {
q076_MinimumWindowSubstring solution = new q076_MinimumWindowSubstring();
System.out.println(solution.minWindow("ADOBECODEBANC", "BANC"));
int i = 0;
System.out.println(i++ == 1);
}
/**
* ½â·¨1(ÍÆ¼ö½â·¨)
* @param s
* @param t
* @return
*/
public String minWindow(String s, String t) {
int[] tmap = new int[128];
for (int c : t.toCharArray()) tmap[c]++;
int count = t.length(), begin = 0, end = 0, minLen = Integer.MAX_VALUE, leftMin = 0;
while (end < s.length()) {
if (tmap[s.charAt(end++)]-- > 0) count--;
while ((count == 0)) {
if (end - begin < minLen) minLen = end - (leftMin = begin);
if (++tmap[s.charAt(begin++)] > 0) count++;
}
}
return minLen == Integer.MAX_VALUE ? "" : s.substring(leftMin, leftMin + minLen);
}
}