-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum Window Substring.cpp
46 lines (41 loc) · 1.37 KB
/
Minimum Window Substring.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
// https://oj.leetcode.com/problems/minimum-window-substring/
class Solution {
public:
string minWindow(string S, string T) {
int ssize = S.size(), tsize = T.size();
if (ssize < tsize || ssize == 0) {
return "";
}
int wbegin = 0;
int win_len = INT_MAX;
int win_begin = -1;
int expect[256], actual[256];
int nr_covered = 0;
memset(expect, 0, 256 * sizeof(int));
memset(actual, 0, 256 * sizeof(int));
for (int i = 0; i < tsize; i++) {
expect[T[i]]++;
}
for (int wend = 0; wend < ssize; wend++) {
if (expect[S[wend]] > 0) {
actual[S[wend]]++;
if (actual[S[wend]] <= expect[S[wend]]) {
nr_covered++;
}
}
if (nr_covered == tsize) {
// move behind pointer forward
while (expect[S[wbegin]] == 0
|| expect[S[wbegin]] < actual[S[wbegin]]) {
actual[S[wbegin]]--;
wbegin++;
}
if (win_len > wend - wbegin + 1) {
win_len = wend - wbegin + 1;
win_begin = wbegin;
}
}
}
return (win_len == INT_MAX) ? "" : S.substr(win_begin, win_len);
}
};