滑动窗口问题

727. Minimum Window Subsequence

题目链接
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 of S will be in the range [1, 20000].
The length of T will be in the range [1, 100].

题目大意
给定字符串S和T,在S中寻找最小子串W,使得T是W的子序列。

解题思路
状态

  • dp[i][j] 表示S中前i个字符包含T中前j个字符的起始位置。

转移方程

  • 如果S[i - 1]字符等于T[j - 1],那么起始位置可以参考dp[i - 1][j - 1]
  • 如果不等,那么起始位置参考dp[i - 1][j]

复杂度
TC: O(n^2)
SC: O(n^2)

public String minWindow(String S, String T) {
    int m = S.length(), n = T.length(), start = -1, minLen = Integer.MAX_VALUE;
    int[][] dp = new int[m + 1][n + 1];
    for (int[] d: dp) Arrays.fill(d, -1);

    for (int i = 0; i <= m; ++i) dp[i][0] = i; //匹配空字符在S中的起始位置

    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= Math.min(i, n); ++j) { //t需要匹配的长度总小于s
            dp[i][j] = S.charAt(i - 1) == T.charAt(j - 1) ? dp[i - 1][j - 1] : dp[i - 1][j];
        }
        if (dp[i][n] != -1) {
            int len = i - dp[i][n];
            if (minLen > len) {
                minLen = len;
                start = dp[i][n];
            }
        }
    }

    //printMatrix(dp);
    return (start != -1) ? S.substring(start, start + minLen) : "";
}

76. Minimum Window Substring

题目链接
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

Example:

Input: S = “ADOBECODEBANC”, T = “ABC”
Output: “BANC”

Note:

If there is no such window in S that covers all characters in T, return the empty string "".
If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

题目大意
在S中找出包含T的全部字符的最小子串。

解题思路
滑动窗口

复杂度
TC: O(n)
SC: O(256)

public String minWindow(String s, String t) {
    int minLen = Integer.MAX_VALUE;
    
    int cnt = t.length();

    int[] map = new int[256];
    int start = -1;
    
    for (int i = 0; i < t.length(); i++){
        map[t.charAt(i)]++;
    }
    
    
    int left= 0;
    int right = 0;
    
    while(right < s.length()){
        if(map[s.charAt(right)]-- > 0){
            cnt--;
        }
        while(cnt == 0){
            int curLen = right - left + 1;
            if (curLen < minLen){
                minLen = curLen;
                start = left;
            }
            if (map[s.charAt(left++)]++ >= 0){
                cnt++;
            }
        }
        right++;
    }
    
    return start == -1? "":s.substring(start, start + minLen);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值