Similar idea, but I start thinking by the substring ending index, so it would be kind of redundant at return statement, and I use INT_MAX as sentinel instead of -1.

```
string minWindow(string S, string T) {
int m = S.size(), n = T.size(), res = INT_MAX, tail;
vector<vector<int>> dp(m+1, vector<int>(n+1, INT_MAX));
for(int i = 0; i <= m; i++) dp[i][0] = 0;
for(int i = 1; i <= m; i++){
for(int j = 1; j <= i && j <= n; ++j){
if(S[i-1] != T[j-1])
dp[i][j] = dp[i-1][j] == INT_MAX? INT_MAX : dp[i-1][j]+1;
else{
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j]);
if(dp[i][j] != INT_MAX) dp[i][j]++;
}
}// for j
}// for i
for(int i = m; i >= 0; --i){
if(dp[i][n] <= res){
res = dp[i][n];
tail = i;
}
}
return res == INT_MAX ? "" : S.substr(tail - res, res);
}
```