|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * 727. Minimum Window Subsequence |
| 7 | + * |
| 8 | + * Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W. |
| 9 | + * If there is no such window in S that covers all characters in T, |
| 10 | + * return the empty string "". If there are multiple such minimum-length windows, return the one with the left-most starting index. |
| 11 | +
|
| 12 | + Example 1: |
| 13 | + Input: |
| 14 | + S = "abcdebdde", T = "bde" |
| 15 | + Output: "bcde" |
| 16 | +
|
| 17 | + Explanation: |
| 18 | + "bcde" is the answer because it occurs before "bdde" which has the same length. |
| 19 | + "deb" is not a smaller window because the elements of T in the window must occur in order. |
| 20 | +
|
| 21 | + Note: |
| 22 | + All the strings in the input will only contain lowercase letters. |
| 23 | + The length of S will be in the range [1, 20000]. |
| 24 | + The length of T will be in the range [1, 100]. |
| 25 | + */ |
| 26 | +public class _727 { |
| 27 | + public static class Solution1 { |
| 28 | + /** |
| 29 | + * This naive brute force results in TLE. |
| 30 | + */ |
| 31 | + public String minWindow(String S, String T) { |
| 32 | + String result = S; |
| 33 | + for (int i = 0; i < S.length(); i++) { |
| 34 | + for (int j = i + T.length(); j <= S.length(); j++) { |
| 35 | + String sub = S.substring(i, j); |
| 36 | + if (sub.length() < result.length() && isSubsequence(T, sub)) { |
| 37 | + result = sub; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return result.equals(S) ? "" : result; |
| 42 | + } |
| 43 | + |
| 44 | + private boolean isSubsequence(String T, String sub) { |
| 45 | + int i = 0; |
| 46 | + for (int j = 0; i < T.length() && j < sub.length(); j++) { |
| 47 | + if (T.charAt(i) == sub.charAt(j)) { |
| 48 | + i++; |
| 49 | + } |
| 50 | + } |
| 51 | + return i == T.length(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static class Solution2 { |
| 56 | + /**credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_727.java*/ |
| 57 | + public String minWindow(String S, String T) { |
| 58 | + int[][] dp = new int[S.length() + 1][T.length() + 1]; |
| 59 | + int INFINITY = 1 << 29; |
| 60 | + Arrays.fill(dp[0], INFINITY); |
| 61 | + dp[0][0] = 0; |
| 62 | + for (int i = 1; i <= S.length(); i++) { |
| 63 | + for (int j = 1; j <= T.length(); j++) { |
| 64 | + dp[i][j] = dp[i - 1][j] + 1; |
| 65 | + if (S.charAt(i - 1) == T.charAt(j - 1)) { |
| 66 | + dp[i][j] = dp[i - 1][j - 1] + 1; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + int ans = INFINITY; |
| 71 | + int tail = -1; |
| 72 | + for (int i = 1; i <= S.length(); i++) { |
| 73 | + if (dp[i][T.length()] < ans) { |
| 74 | + ans = dp[i][T.length()]; |
| 75 | + tail = i; |
| 76 | + } |
| 77 | + } |
| 78 | + return ans == INFINITY ? "" : S.substring(tail - ans, tail); |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | +} |
0 commit comments