-
Notifications
You must be signed in to change notification settings - Fork 3
/
727.cpp
35 lines (32 loc) · 1.22 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
class Solution {
public:
string minWindow(string s1, string s2) {
int m = s1.size();
int n = s2.size();
// dp[i][j]: the starting index of the substring where T has length i and S has length j.
// if T[i - 1] == S[j - 1], this means we could borrow the start index from dp[i - 1][j - 1] to make the current substring valid;
// else, we only need to borrow the start index from dp[i][j - 1] which could either exist or not.
vector<vector<int>> dp(m + 1, vector(n + 1, -1));
for (int i = 0; i <= m; ++i) dp[i][0] = i;
int minLen = INT_MAX;
int start = -1;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (j > i) continue;
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
else dp[i][j] = dp[i - 1][j];
}
if (dp[i][n] != -1) {
int len = i - dp[i][n];
if (len < minLen) {
minLen = len;
start = dp[i][n];
}
}
}
if (start == -1) return "";
return s1.substr(start, minLen);
}
};