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.
Java Solution 1 – Two pointers
In a brute-force way, this problem can be solved by using two pointers which iterate over characters of the two strings respectively.
public String minWindow(String S, String T) { int start=0; String result = ""; while(start<S.length()){ int j=0; for(int i=start; i<S.length(); i++){ if(S.charAt(i)==T.charAt(j)&&j==0){ start=i; } if(S.charAt(i)==T.charAt(j)){ j++; } if(j==T.length()){ if(result.equals("")||(i-start+1)<result.length()){ result = S.substring(start, i+1); } start=start+1; break; } if(i==S.length()-1){ return result; } } } return result; } |