forked from chuyi2007/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.java
More file actions
83 lines (79 loc) · 2.66 KB
/
MinimumWindowSubstring.java
File metadata and controls
83 lines (79 loc) · 2.66 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//O(N) solution
public class Solution {
public String minWindow(String S, String T) {
// Start typing your Java solution below
// DO NOT write main() function
int min = S.length() + 1;
int[] match = new int[256];
int[] target = new int[256];
for(int i = 0; i < T.length(); ++i){
++target[(int)T.charAt(i)];
}
int count = T.length();
int start = -1, end = -1;
//i as start index, j as end index
for(int i = 0, j = 0; i <= j && j <= S.length();){
if(count > 0){
if(j == S.length())
break;
char ce = S.charAt(j);
if(T.contains(String.valueOf(ce))){
++match[(int)ce];
if(match[(int)ce] <= target[(int)ce])
--count;
}
++j;
}
if(count == 0){
char cs = S.charAt(i);
int len = j - i;
if(len < min){
min = len;
start = i;
end = j;
}
if(T.contains(String.valueOf(cs))){
--match[(int)cs];
if(match[(int)cs] < target[(int)cs])
++count;
}
++i;
}
}
if(start == -1)
return "";
else
return S.substring(start, end);
}
}
//O(N * N * M) solution
public class Solution {
public String minWindow(String S, String T) {
// Start typing your Java solution below
// DO NOT write main() function
int min = S.length() + 1;
int start = -1, end = -1;
for(int i = 0; i < S.length(); ++i){
for(int j = i + T.length(); j <= S.length(); ++j){
String tmp = new String(T);
for(int k = i; k < j; ++k){
String cur = S.substring(k, k + 1);
if(tmp.contains(cur))
tmp = tmp.substring(0, tmp.indexOf(cur))
+ tmp.substring(tmp.indexOf(cur) + 1);
}
if(tmp.length() == 0 && j - i < min){
min = j - i;
start = i;
end = j;
//if found one, no need to increase j, because must be larger
break;
}
}
}
if(start == -1)
return "";
else
return S.substring(start, end);
}
}