LC 727. Minimum Window Subsequence 【lock,hard】
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
Swill be in the range[1, 20000]. - The length of
Twill 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】的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- LeetCode——727.Minimum Window Subsequence
一.题目链接:https://leetcode.com/problems/minimum-window-substring/ 二.题目大意: 给定两个字符串S和T,要求从S中找出包含T中所有字母的最短 ...
- LC 425. Word Squares 【lock,hard】
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【转】全志A10/A20 Bootloader加载过程分析
原文 : http://blog.csdn.net/allen6268198/article/details/12905425 从这里开始:http://linux-sunxi.org/Bootabl ...
- Mysql(四)-1:单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...
- 6.JVM技术_java内存模型
1.java内存模型图解 Java虚拟机在执行Java程序的过程中,会把它所管理的内存划分为若干个不同的数据区.这些区域有各自的用途,以及创建和销毁的 时间,有的区域随着虚拟机进程的启动而存在,有的区 ...
- Beta冲刺——星期五
这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 飞猪们 这个作业的目标 剩余任务预估,分配任务(开发,测试等).按要求提交当天冲刺报告. ...
- DP tricks and experiences
[LeetCode] 关于动态规划的经验与技巧. 很多时候多分配一位空间是为了递推的方便,例如前推两位. 辅助数组的索引,用到的可能是 1 — N,或者是 0 — N-1. 具体要看清 f[i] 每一 ...
- Invalid character found in the request target. The valid characters are defined in RFC 7230 and RF
SpringBoot 请求参数包含 [] 特殊符号 返回400状态 //springBoot 启动类 添加 bean @Bean public TomcatServletWebServerFactor ...
- LeetCode 69 x 的平方根
链接:https://leetcode-cn.com/problems/sqrtx 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数, ...
- IIFE 立即执行函数表达式-模块化
1)立即执行 2)表达式 3)括号,分号结束 | 前缀运算符 | 表达式上下文 4)只需要执行一次,内部变量无需维护,可用于初始化 (function( ) { })( ); 或 (function( ...
- SQL进程死锁排查
--进程执行状态 SELECT der.[session_id],der.[blocking_session_id], sp.lastwaittype,sp.hostname,sp.program_n ...
- jquery实现input输入框点击加减数值随之变动
<input class="addBtn min" type="button" value="-" /><input cl ...