Given strings S
and T
, find the minimum (contiguous) substring W
of S
, so that T
is a subsequence of W
.
If there is no such window in S
that covers all characters in T
, return the empty string ""
. If there are multiple such minimum-length windows, return the one with the left-most starting index.
Example 1:
Input: S = "abcdebdde", T = "bde" Output: "bcde" Explanation: "bcde" is the answer because it occurs before "bdde" which has the same length. "deb" is not a smaller window because the elements of T in the window must occur in order.
Note:
All the strings in the input will only contain lowercase letters.The length ofS
will be in the range
[1, 20000]
.The length of
T
will be in the range
[1, 100]
.
思路:用过二分,TLE,想用DP,但是不知道怎么构造DP数组的含义,看discuss才AC,据说Brute force也能AC?
package l727;
// 二分
class TLE {
public String minWindow(String S, String T) {
char[] cs = S.toCharArray();
char[] ct = T.toCharArray();
int lo = T.length(), hi = S.length();
int min = Integer.MAX_VALUE, start = -1;
while(lo < hi) {
int mid = (lo+hi)/2;
int t = ok(cs, ct, mid);
if(t != -1) {
min = mid;
start = t;
hi = mid;
} else {
lo = mid+1;
}
}
return min==Integer.MAX_VALUE ? "" : S.substring(start, start+min);
}
private int ok(char[] cs, char[] ct, int mid) {
for(int i=0; i<cs.length-mid+1; i++) {
if(isSeq(cs, i, i+mid-1, ct))
return i;
}
return -1;
}
private boolean isSeq(char[] cs, int s, int t, char[] ct) {
int i = s, j = 0;
while(i <= t) {
while(i<=t && cs[i]!=ct[j]) i++;
if(i > t) break;
i++;
j++;
if(j == ct.length) return true;
}
return false;
}
}
dp是要连续2层循环在一起才可以状压
package l727;
/*
* 如果不以什么什么结尾的话,即使求出了dp数组也求不出答案
* dp[i][j]=k表示T[0..j]是S[k..i]的subsequence,且S[k..i]是最短的(隐含T[0]=S[k]),
* 且强制要求T[j]=S[i],否则dp[i][j]=-1
*
* 之前有先考虑start的,有先考虑end-start的,这里好像是先考虑end
*/
class TLE_DP {
public String minWindow(String S, String T) {
char[] cs = S.toCharArray();
char[] ct = T.toCharArray();
int[][] dp = new int[cs.length][ct.length];
for(int i=0; i<cs.length; i++) {
dp[i][0] = -1;
if(cs[i] == ct[0])
dp[i][0] = i;
}
for(int i=0; i<cs.length; i++) {
for(int j=1; j<ct.length; j++) {
dp[i][j] = -1;
if(cs[i] == ct[j]) {
for(int k=0; k<i; k++)
dp[i][j] = Math.max(dp[i][j], dp[k][j-1]);
}
}
}
int minLen = Integer.MAX_VALUE, start = -1;
for(int i=0; i<cs.length; i++) {
if(dp[i][ct.length-1] != -1) {
if(i-dp[i][ct.length-1]+1 < minLen) {
minLen = i-dp[i][ct.length-1]+1;
start = dp[i][ct.length-1];
} else if(i-dp[i][ct.length-1]+1 == minLen) {
start = Math.min(start, dp[i][ct.length-1]);
}
}
}
return minLen == Integer.MAX_VALUE ? "" : S.substring(start, start+minLen);
}
}
package l727;
/*
* 如果不以什么什么结尾的话,即使求出了dp数组也求不出答案
* dp[i][j]=k表示T[0..j]是S[k..i]的subsequence,且S[k..i]是最短的(隐含T[0]=S[k]),
* 且强制要求T[j]=S[i],否则dp[i][j]=-1
*
* 之前有先考虑start的,有先考虑end-start的,这里好像是先考虑end
*
*/
class DP_AC {
public String minWindow(String S, String T) {
char[] cs = S.toCharArray();
char[] ct = T.toCharArray();
int[][] dp = new int[cs.length][ct.length];
for(int i=0; i<cs.length; i++) {
dp[i][0] = -1;
if(cs[i] == ct[0])
dp[i][0] = i;
}
// 如果先循环j的话,只需要2层循环,用一个变量缓存一下
for(int j=1; j<ct.length; j++) {
int k = -1;
for(int i=0; i<cs.length; i++) {
dp[i][j] = -1;
if(k!=-1 && cs[i] == ct[j]) dp[i][j]=k;
if(dp[i][j-1] != -1) k = dp[i][j-1];
}
}
int minLen = Integer.MAX_VALUE, start = -1;
for(int i=0; i<cs.length; i++) {
if(dp[i][ct.length-1] != -1) {
if(i-dp[i][ct.length-1]+1 < minLen) {
minLen = i-dp[i][ct.length-1]+1;
start = dp[i][ct.length-1];
} else if(i-dp[i][ct.length-1]+1 == minLen) {
start = Math.min(start, dp[i][ct.length-1]);
}
}
}
return minLen == Integer.MAX_VALUE ? "" : S.substring(start, start+minLen);
}
}
Approach #1: Dynamic Programming [Accepted]
Intuition
A naive brute force is relatively easy: for each starting position S[i]
, scan left to right trying to match elements T[j]
in order. Unfortunately, this is O(S^2)O(S2) complexity, so we seek to improve it.
When matching S[i]
to some element of T
, we can only be in one of T.length
states (instead of S.length
of them), based on how many letters are left in T
to match. Thus, this is a natural area to try to reduce repeated work, and motivates the idea try for dynamic programming on T.length
instead.
Algorithm
At time j
, for each position e
in S
(e
for end), let's remember the largest index cur[e] = s
(for start) so that S[s: e+1]
has T[:j]
as a subsequence, and -1
(or None
) otherwise if it isn't possible.
To update our knowledge as j += 1
, if S[i] == T[j]
, then new[e]
is last
, the largest s
we have seen so far (representing that T[:j]
was found). We can prove this is just the most recent valid index we have seen.
At the end, we find the best answer: cur[e] = s
means there was a window S[s: e+1]
. In Python, we use cur
and new
, while in Java we use dp[j]
and dp[~j]
to keep track of the last two rows of our dynamic programming.
class Solution {
public String minWindow(String S, String T) {
int[][] dp = new int[2][S.length()];
for (int i = 0; i < S.length(); ++i)
dp[0][i] = S.charAt(i) == T.charAt(0) ? i : -1;
for (int j = 1; j < T.length(); ++j) {
int last = -1;
Arrays.fill(dp[j & 1], -1);
for (int i = 0; i < S.length(); ++i) {
if (last >= 0 && S.charAt(i) == T.charAt(j))
dp[j & 1][i] = last;
if (dp[~j & 1][i] >= 0)
last = dp[~j & 1][i];
}
}
int start = 0, end = S.length();
for (int e = 0; e < S.length(); ++e) {
int s = dp[~T.length() & 1][e];
if (s >= 0 && e - s < end - start) {
start = s;
end = e;
}
}
return end < S.length() ? S.substring(start, end+1) : "";
}
}
Normal brute force complexity is also the same O(ST). I wonder if time complexity could be better than this.
My Brute force solution
class Solution {
public String minWindow(String S, String T) {
int min=-1,idx=-1;
char[] Tc=T.toCharArray();
char[] Sc=S.toCharArray();
for(int i=0;i<S.length();i++){
if(Sc[i]!=Tc[0]) continue;
int len=check(Tc,Sc,i);
if(len<=0) break;
if(min==-1 || len<min){
idx=i;
min=len;
}
}
if(min==-1) return "";
return S.substring(idx,idx+min);
}
public int check(char[] Tc,char[] Sc, int start){
int i=start,j=0;
while(i<Sc.length && j<Tc.length){
if(Sc[i]==Tc[j]) j++;
i++;
}
if(j==Tc.length) return i-start;
return -1;
}
}