forked from mengli/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum Window Substring.java
More file actions
54 lines (45 loc) · 1.7 KB
/
Minimum Window Substring.java
File metadata and controls
54 lines (45 loc) · 1.7 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
48
49
50
51
52
53
54
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,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
Note:
If there is no such window in S that covers all characters in T, return the emtpy string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
public class Solution {
public String minWindow(String S, String T) {
int sLen = S.length();
int tLen = T.length();
int[] needToFind = new int[256];
for (int i = 0; i < tLen; i++)
needToFind[T.charAt(i)]++;
int[] hasFound = new int[256];
int minWindowLen = Integer.MAX_VALUE;
int minWindowBegin = 0;
int minWindowEnd = 0;
int count = 0;
for (int begin = 0, end = 0; end < sLen; end++) {
// skip characters not in T
if (needToFind[S.charAt(end)] == 0) continue;
hasFound[S.charAt(end)]++;
if (hasFound[S.charAt(end)] <= needToFind[S.charAt(end)]) count++;
// if window constraint is satisfied
if (count == tLen) {
// advance begin index as far right as possible,
// stop when advancing breaks window constraint.
while (needToFind[S.charAt(begin)] == 0 || hasFound[S.charAt(begin)] > needToFind[S.charAt(begin)]) {
if (hasFound[S.charAt(begin)] > needToFind[S.charAt(begin)]) hasFound[S.charAt(begin)]--;
begin++;
}
// update minWindow if a minimum length is met
int windowLen = end - begin + 1;
if (windowLen < minWindowLen) {
minWindowBegin = begin;
minWindowEnd = end;
minWindowLen = windowLen;
} // end if
} // end if
} // end for
return (count == tLen) ? S.substring(minWindowBegin, minWindowEnd + 1) : "";
}
}