-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.cpp
More file actions
57 lines (46 loc) · 1.27 KB
/
MinimumWindowSubstring.cpp
File metadata and controls
57 lines (46 loc) · 1.27 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
class Solution {
public:
string minWindow(string S, string T) {
int strT[255];
int strC[255];
memset(strT, 0, sizeof(strT));
memset(strC, 0, sizeof(strC));
for (auto c : T) {
++strT[c];
++strC[c];
}
int lenS = S.length();
int lenT = T.length();
int lhs = 0;
int rhs = 0;
string result;
for (NULL; rhs < lenS; ++rhs) {
if (strT[S[rhs]] > 0) {
--strC[S[rhs]];
if (strC[S[rhs]] >= 0) {
--lenT;
}
}
if (lenT == 0) {
while (lhs <= rhs) {
if (strT[S[lhs]] == 0) {
++lhs;
continue;
}
if (strC[S[lhs]] < 0) {
++strC[S[lhs]];
++lhs;
continue;
}
else {
break;
}
}
if (rhs - lhs + 1 < result.length() || result.length() == 0) {
result = S.substr(lhs, rhs - lhs + 1);
}
}
}
return result;
}
};