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. Qt5配置winpCap

    在网上查了很多资料,搞了差不多一天总算解决Qt5使用winPcap配置的问题了!记录一下 以便后续忘记 1.下载winpcap4.1.3,百度即可搜索到 2.下载winpCap开发者工具包http:/ ...

  2. cubase 反向音频处理

  3. FLUSH TABLES WITH READ LOCK 获取锁的速度

    最近有一台MySQL的从库老是报延迟,观察到:FLUSH TABLES WITH READ LOCK,阻塞了4个多小时,还有另外一条SQL语句select *,从现象上来看是select * 阻塞了f ...

  4. VS2008配合SQLite开发WINCE、PDA智能设备项目环境搭设。

    1.安装vs2008 ---------------------------vs2008上安装TFS步骤(详细请见——http://www.cnblogs.com/mayt/archive/2013/ ...

  5. Win10 家庭版找不到 gpedit.msc;win10怎样关闭windows defender

    家庭版本是自带策略组相关文件的,只需自定义一个txt,输入下面代码,再改成cmd 后缀格式文件.然后右键,管理员权限运行即可 @echo offpushd "%~dp0"dir / ...

  6. App支付宝登录授权

    一.在支付宝开放平台申请App应用,并且配置后台信息 https://openhome.alipay.com/platform/appManage.htm#/apps 填写自己的申请信息 添加应用功能 ...

  7. 小程序wx.showToast()方法实现文字换行

    小程序wx.showToast()方法实现文字换行 在文字中间加上  '\r\n' 真机中生效 wx.showToast({ title: `换行前内容\r\n换行后内容`, icon: 'none' ...

  8. SiteMesh的简单使用

    简介: SiteMesh是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的. 它能通过创建一个包装对象,也就是装饰来包裹的对象.尽管它是由Java语言来实现的, ...

  9. keil中使用Astyle格式化你的代码的方法-keil4 keil5通用

    简介:在给RTT 提交代码,需要符合RT-Thread 的代码规范,本文简单介绍如何使用Astyle 格式化为符合RTT要求的代码风格. 关于Astyle Astyle 的全称是Artistic St ...

  10. C#窗体的resx文件

    这些图片在项目文件中没找到,原来都存在了resx文件中. 属性界面的Image.BackgroundImage属性手动选择的图片会自动存储到resx文件中,之后这些图片源文件就可以删除了.resx中的 ...