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. python面向编程;类的绑定与非绑定方法、反射、内置方法

    一.类的绑定与非绑定方法 ''' 类中定义函数分为了两大类: 1. 绑定方法 特殊之处: 绑定给谁就应该由谁来调用,谁来调用就会将谁当做第一个参数自动传入 绑定给对象的方法: 在类中定义函数没有被任何 ...

  2. 1.java多线程_实现线程的两种方式

    1.java多线程基本知识 1.1.进程介绍 不管是我们开发的应用程序,还是我们运行的其他的应用程序,都需要先把程序安装在本地的硬盘上.然后找到这个程序的启动文件, 启动程序的时候,其实是电脑把当前的 ...

  3. Android异常与性能优化相关面试问题-冷启动优化面试问题详解

    什么是冷启动: 冷启动的定义:冷启动就是在启动应用前,系统中没有该应用的任何进程信息.实际也就是要执行Application.onCreate()方法的那次启动. 冷启动 / 热启动的区别:热启动:用 ...

  4. JS特殊写法

    记录下工作中碰到的JS特殊写法 (function(index) { $('#' + id).on("change", function() { me.onChange(this, ...

  5. JAVA_GET请求URL

    import java.io.IOException; import net.sf.json.JSONObject; import org.apache.commons.httpclient.Defa ...

  6. laya入门及egret

    laya篇 1.laya的资源会自动打包可手动设置,在view编辑界面 2.laya的界面编辑器用的是mornUI,所以是xml解析成view的 3.laya的事件均继承dom事件 4.sprite的 ...

  7. [Linux]ubuntu16.04 nginx彻底删除与重新安装

    nginx彻底删除与重新安装 查看nginx正在运行的进程,如果有就kill掉 sudo netstat -ntlp | grep nginx sudo kill -9 进程id 删除nginx,pu ...

  8. MySecurity(安全类)

    /// <summary> /// MySecurity(安全类) 的摘要说明. /// </summary> public class MySecurity { /// &l ...

  9. 图片文件打开失败:“Could not load image... ...0x89 0x50”

    造冰箱的熊猫@cnblogs 2018/12/10 使用Image Viewer打开图片文件时,提示错误“Could not load image '001.jpg'. Error interpret ...

  10. Linux—查看远程Linux系统运行时间

    [选择题]在Shell环境下,如何查看远程Linux系统运行了多少时间? A.scp user@被监控主机ip "uptime" B.ssh user@被监控主机ip " ...