-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path76.cpp
49 lines (43 loc) · 1.12 KB
/
76.cpp
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
class Solution {
public:
string minWindow(string s, string t) {
if (s.size() < t.size()) {
return "";
}
int appearancesT[256] = {0};
for (int i = 0; i < t.size(); i++) {
appearancesT[t[i]]++;
}
int start = 0;
int startIndex = -1;
int min = INT_MAX;
int count = 0;
int appearancesS[256] = {0};
for (int i = 0; i < s.size(); i++) {
appearancesS[s[i]]++;
if (appearancesS[s[i]] <= appearancesT[s[i]]) {
count++;
}
// cout << count << endl;
if (count == t.size()) {
// cout << "test:" << start << " " << i << endl;
while (appearancesS[s[start]] > appearancesT[s[start]] ||
appearancesT[s[start]] == 0) {
if (appearancesS[s[start]] > appearancesT[s[start]]) {
appearancesS[s[start]]--;
}
start++;
}
// cout << "test2:" << start << " " << i << endl;
if (min > i - start + 1) {
min = i - start + 1;
startIndex = start;
}
}
}
if (startIndex == -1) {
return "";
}
return s.substr(startIndex, min);
}
};