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 ...
随机推荐
- deep_learning_MNIST数据集
Code_link:https://pan.baidu.com/s/1dshQt57196fhh67F8nqWow 本文是为既没有机器学习基础也没了解过TensorFlow的码农.序媛们准备的.如果已 ...
- win10软件使用指南备忘录
altrun:http://xbeta.info/altrun.htm timer:https://www.playpcesor.com/2009/04/timer.html (好像要上网打开) do ...
- three.js之性能监视器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- c++四种分配内存的方法整理
1 calloc 函数: void *calloc(unsigned int num, unsigned int size) 按照所给的数据个数和数据类型所占字节数,分配一个 num * size 连 ...
- centos7 搭建pxe 安装centos windows(非全自动)(这个教程测试centos6和7.2可以用,Windows各版本也可以)
yum install dhcp xinetd syslinux tftp-server httpd 编辑dhcpdb配置(192.168.0.1为本机IP) ; max-lease-time ; l ...
- Java#Spring框架下注解解析
@Bean 定义Bean @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 @Configura ...
- kotlin面向对象入门
之前在学kotlin基础语法时咱们是采用三方jar包在eclipse工程下进行的,很显然这工具在实际商用中基本上很少用到了,最终是要编写android程序,所以说从这里起得更换一个更加智能更加贴近实际 ...
- Echarts 饼状图 字体重叠问题
原理:设置最小扇形的大小,把他撑起来 在 series 里 使用 minAngle: 38, //最小的扇区角度(0 ~ 360),用于防止某个值过小导致扇区太小影响交互 角度自己调好就可以了 个人笔 ...
- Dom修改元素样式
提纲:我们可以通过js来修改元素的大小,颜色,位置等样式 1.element.style 行内样式的操作 2.element.className ...
- MySQL BinLog Server 搭建实战
一.MySQL Binlog server 介绍 MySQL Binlog Server: 它使用 mysqlbinlog 命令以 daemon 进程的方式模拟一个 slave 的 IO 线程与主库连 ...