-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
727.cpp
47 lines (47 loc) · 1.68 KB
/
727.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
__________________________________________________________________________________________________
class Solution {
public:
string minWindow(string S, string T) {
int m = S.size(), n = T.size(), start = -1, minLen = INT_MAX;
vector<vector<int>> dp(m + 1, vector<int>(n + 1, -1));
for (int i = 0; i <= m; ++i) dp[i][0] = i;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= min(i, n); ++j) {
dp[i][j] = (S[i - 1] == T[j - 1]) ? dp[i - 1][j - 1] : dp[i - 1][j];
}
if (dp[i][n] != -1) {
int len = i - dp[i][n];
if (minLen > len) {
minLen = len;
start = dp[i][n];
}
}
}
return (start != -1) ? S.substr(start, minLen) : "";
}
};
__________________________________________________________________________________________________
class Solution {
public:
string minWindow(string S, string T) {
int m = S.size(), n = T.size(), start = -1, minLen = INT_MAX, i = 0, j = 0;
while (i < m) {
if (S[i] == T[j]) {
if (++j == n) {
int end = i + 1;
while (--j >= 0) {
while (S[i--] != T[j]);
}
++i; ++j;
if (end - i < minLen) {
minLen = end - i;
start = i;
}
}
}
++i;
}
return (start != -1) ? S.substr(start, minLen) : "";
}
};
__________________________________________________________________________________________________