Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
83 lines (68 loc) · 2.62 KB

File metadata and controls

83 lines (68 loc) · 2.62 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package Algorithms;
import java.util.HashMap;
public class MinimumWindowSubString {
public static void main(String[] strs) {
System.out.println(MinimumWindowSubString.minWindow("a", "a"));
}
public static String minWindow(String S, String T) {
if (S == null || T == null || T.length() == 0 || S.length() == 0) {
return "";
}
int resultLen = -1;
int resultleft = 0;
int sum = 0;
HashMap<Character, Integer> setSub = new HashMap<Character, Integer>();
for (int i = 0; i < T.length(); i++) {
if (setSub.containsKey(T.charAt(i))) {
setSub.put(T.charAt(i), setSub.get(T.charAt(i)) + 1);
} else {
setSub.put(T.charAt(i), 1);
}
}
// count the number of characters in String T;
sum = setSub.size();
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// record the left board.
int left = 0;
// record the
//int equal = sum;
for (int i = 0; i < S.length(); i++) {
if (setSub.containsKey(S.charAt(i))) {
if (map.containsKey(S.charAt(i))) {
map.put(S.charAt(i), map.get(S.charAt(i)) + 1);
} else {
map.put(S.charAt(i), 1);
}
if (map.get(S.charAt(i)) == setSub.get(S.charAt(i))) {
sum--;
}
}
// contains all the charaters.
if (0 == sum) {
while (true) {
Integer number = map.get(S.charAt(left));
// it should contains at least the number in T
if (number != null && number <= setSub.get(S.charAt(left))) {
map.put(S.charAt(left), number - 1);
sum++;
break;
}
if (number != null) {
map.put(S.charAt(left), number - 1);
}
left++;
}
if (resultLen == -1 || resultLen > i - left + 1) {
resultLen = i - left + 1;
resultleft = left;
left++;
}
}
}
if (resultLen == -1) {
return "";
} else {
return S.substring(resultleft, resultleft + resultLen);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.