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].

解题思路:

仔细读题,注意这里说的是:子序列!!!

子序列意味着每个字符之间的相对顺序不能够改变。

解法参考了zestypanda的解法。

是用动态规划来进行解答。

首先搞清楚dp[j][i]的含义: 对于目标字符串T下标为[0,j]的子串作为子序列出现在给定字符串S[0, i]中的起始位置

初始化dp[0][i]:当S[i] == T[0]时,dp[0][i] = i, else dp[0][i] = -1;

从T的第二个字符开始遍历。

我们用k记录对于T[0, j-1]在当前S[i]之前出现的下标。

当dp[j-1][i] != -1的时候表示到当前i,出现了T[0,j-1]的子序列。将其存入k

若S[i] == T[j], 且 k != -1,表示对于T[0, j]作为子序列出现了。更新dp[j][i].

现在dp中存储的是起始位置。但是我们想要知道的是长度最小的子串。

所以我们遍历dp[n-1][i] (因为要包含T作为子串),找到起始坐标start,子串长度i-start+1最小。

最后需要判断子串是否存在:

若start = -1,则不存在。

此时的空间复杂度为O(mn),可以通过滚动数组的方式,将其优化至O(m)

代码:

class Solution {
public:
    string minWindow(string S, string T) {
        int m = S.size(), n = T.size();
        vector<vector<int>> dp(n, vector<int>(m, -1));
        for(int i = 0; i < m; i++){
            if(S[i] == T[0]) dp[0][i] = i;
        }

        for(int j = 1; j < n; j++){
            int k = -1;
            for(int i = 0; i < m; i++){
                if(k != -1 && S[i] == T[j]) dp[j][i] = k;
                if(dp[j-1][i] != -1) k = dp[j-1][i];
            }
        }

        int start = -1, len = INT_MAX;
        for(int i = 0; i < m; i++){
            if(dp[n-1][i] != -1 && i - dp[n-1][i]+1 < len){
                start = dp[n-1][i];
                len = i - dp[n-1][i]+1;
            }
        }
        return start == -1 ? "":S.substr(start, len);
    }
};

原文地址:https://www.cnblogs.com/yaoyudadudu/p/9344791.html

时间: 2024-11-15 10:28:21

727. Minimum Window Subsequence的相关文章

[LeetCode] 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 window

Minimum Window Substring, 包含子串的最小窗口,双指针

问题描述:给定字符串S,子串T,求S中包含T的最小窗口 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). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". 算法分析:

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). 思路: Hashtable+双指针 我的代码: public class Solution { public String minWindow(String s, String t) { if(s==null || s.length()==0

Minimum Window Substring leetcode

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). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note:

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

Lintcode32 Minimum Window Substring solution 题解

[题目描述] Given a string source and a string target, find the minimum window in source which will contain all the characters in target. Notice:If there is no such window in source that covers all characters in target, return the emtpy string "".If

76. Minimum Window Substring (Map)

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). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

leetCode(55):Minimum Window Substring(limits.h头文件)

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). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such windo

Java for LeetCode 076 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). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such windo