Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof 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].

Runtime: 44 ms, faster than 73.35% of C++ online submissions for Minimum Window Subsequence.

网上的DP解法。dp定义是能够匹配T[0,i]的最大的S的index.

举个例子,S = babad, T = bad,

i = 0时,只有当j=0,两个相等,所以此时dp = [0, -1, -1]

然后,dp = [0,1,-1],然后, dp = [2, 1, -1], -> dp[2,2,-1] -> dp[2,2,2]

因为时从后往前更新,只有当T的每一个字符都匹配了才能把最开头的index传递到最后。中间即使有些匹配到,如果没有全部匹配也传递不了。

比如 S = babd, T = bad,

dp = [-1,-1,-1] -> dp[0,-1,-1] -> dp[0,1,-1] -> dp[2,1,-1] -> dp[2,1,1] 结果还是1.

class Solution {
public:
string minWindow(string S, string T) {
vector<int> dp(T.length(), -);
string ans = "";
for (int i = ; i < S.length(); i++) {
for (int j = T.length() - ; j >= ; j--) {
if (S[i] == T[j]) {
if (j == ) dp[j] = i;
else dp[j] = dp[j - ];
}
}
int init = dp[T.length() - ];
if (init != - && (ans == "" || i - init + < ans.size())) {
ans = S.substr(init, i - init + );
}
}
return ans;
}
};

这题还有双指针法,过段时间更新。

LC 727. Minimum Window Subsequence 【lock,hard】的更多相关文章

  1. LC 465. Optimal Account Balancing 【lock,hard】

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  2. LC 683. K Empty Slots 【lock,hard】

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  3. [LeetCode] 727. Minimum Window Subsequence 最小窗口序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...

  4. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  5. LeetCode——727.Minimum Window Subsequence

    一.题目链接:https://leetcode.com/problems/minimum-window-substring/ 二.题目大意: 给定两个字符串S和T,要求从S中找出包含T中所有字母的最短 ...

  6. LC 425. Word Squares 【lock,hard】

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  7. LC 774. Minimize Max Distance to Gas Station 【lock,hard】

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  8. LC 272. Closest Binary Search Tree Value II 【lock,hard】

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  9. LC 644. Maximum Average Subarray II 【lock,hard】

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

随机推荐

  1. 彻底卸载mysql数据库~

    以下操作以win10操作系统为例 1 停止window的MySQL服务 打开此台电脑的管理 ---> 服务和应用程序 --->服务,找到mysql的服务并停止 2 卸载MySQL安装程序 ...

  2. fastadmin Excel导出时数字被科学计数

    /public/assets/libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.min.js //exportOpt ...

  3. Samba passwd smbpasswd and tdbsam

    ome commands to convert samba backend password-databases. If you use "passdb backend = smbpassw ...

  4. 语义分割之车道线检测Lanenet(tensorflow版)

    Lanenet 一个端到端的网络,包含Lanenet+HNet两个网络模型,其中,Lanenet完成对车道线的实例分割,HNet是一个小网络结构,负责预测变换矩阵H,使用转换矩阵H对同属一条车道线的所 ...

  5. 06-【servletconfig、servletContext 】

    ServletConfig.ServletContext 1.ServletConfig获取web.xml中的配置信息:java代码: @Override public void init(Servl ...

  6. Hdu 1517 巴什博奕变形

    易知2-9为先手胜 继续递推下去 10-18 后手胜 再推发现19-162先手胜 即发现有9(9) 18(2*9) 162(9*2*9)..... #include<bits/stdc++.h& ...

  7. hdf5文件、tqdm模块、nunique、read_csv、sort_values、astype、fillna

    pandas.DataFrame.to_hdf(self, path_or_buf, key, **kwargs): Hierarchical Data Format (HDF) ,to add an ...

  8. 多个Promise执行顺序

    app.isLogin() // 判断是否登录后 .then(res=>{ this.setData({ login: true }, res2=>{ // 清空临时积分 return a ...

  9. 返回vector指针案例

    void prog1_static(void) { int pos = 9; // elem will hold the element's value vector<int> *elem ...

  10. Jmeter接口测试+压力测试(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/github_27109687/artic ...