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;
}
};

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

最新文章

  1. ubuntu 16 安装django nginx uWSGI
  2. ArrowLayer : A coustom layer animation
  3. 2016HUAS暑假集训训练2 A - Is It A Tree?
  4. TF Boys (TensorFlow Boys ) 养成记(四)
  5. EDIUS分别输出视频和音频的教程
  6. python 学习 异常处理
  7. ajax 处理请求回来的数据
  8. python3.6+selenium3.13 自动化测试项目实战一(增加自动发送邮件报告接口)
  9. Grunt 一个专为JavaScript提供的构建工具
  10. 使用ZooKeeper协调多台Web Server的定时任务处理(方案2)
  11. 分布式一致性算法2PC和3PC
  12. Kali 2.0 下 Metasploit 初始化配置
  13. poj1852 Ants(思维)
  14. JavaScript基本操作之——九个 Console 命令
  15. 20款有用的JavaScript和CSS库
  16. mybatis 3.2.*打印sql结果集
  17. PHP数据库基于PDO操作类(mysql)
  18. odoo开发笔记--模型字段compute用法
  19. Step Detector and Step Counter Sensors on Android
  20. xml属性定义

热门文章

  1. KVM命令记录
  2. Delphi MSCom安装
  3. Oracle笔记(八) 复杂查询及总结
  4. 第1章 python入门
  5. Python twisted事件驱动网络框架 源码剖析
  6. [转载]Spark-Task not serializable错误解析
  7. linux内核 概念
  8. C# 各个版本特征
  9. pycharm运行程序,总是出现IPthony界面(IPython 6.2.1 -- An enhanced Interactive Python. Type &#39;?&#39; for help. PyDev console: using IPython 6.2.1)
  10. mysql中source提高导入数据速率的方法