# [727. Minimum Window Subsequence (Hard)](https://leetcode.com/problems/minimum-window-subsequence)
<p>Given strings <code>s1</code> and <code>s2</code>, return <em>the minimum contiguous substring part of </em><code>s1</code><em>, so that </em><code>s2</code><em> is a subsequence of the part</em>.</p>
<p>If there is no such window in <code>s1</code> that covers all characters in <code>s2</code>, return the empty string <code>""</code>. If there are multiple such minimum-length windows, return the one with the <strong>left-most starting index</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s1 = "abcdebdde", s2 = "bde"
<strong>Output:</strong> "bcde"
<strong>Explanation:</strong>
"bcde" is the answer because it occurs before "bdde" which has the same length.
"deb" is not a smaller window because the elements of s2 in the window must occur in order.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s1 = "jmeqksfrsdcmsiwvaovztaqenprpvnbstl", s2 = "u"
<strong>Output:</strong> ""
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s1.length <= 2 * 10<sup>4</sup></code></li>
<li><code>1 <= s2.length <= 100</code></li>
<li><code>s1</code> and <code>s2</code> consist of lowercase English letters.</li>
</ul>
**Companies**:
[Google](https://leetcode.com/company/google), [Snapchat](https://leetcode.com/company/snapchat), [eBay](https://leetcode.com/company/ebay)
**Related Topics**:
[String](https://leetcode.com/tag/string/), [Dynamic Programming](https://leetcode.com/tag/dynamic-programming/), [Sliding Window](https://leetcode.com/tag/sliding-window/)
**Similar Questions**:
* [Minimum Window Substring (Hard)](https://leetcode.com/problems/minimum-window-substring/)
* [Longest Continuous Increasing Subsequence (Easy)](https://leetcode.com/problems/longest-continuous-increasing-subsequence/)
## Solution 1. DP
Let `dp[i+1][j+1]` be the number of matched letters between `s[0..i]` and `t[0..j]`, `start[i+1][j+1]` be the starting index of the minimum window corresponding to `dp[i+1][j+1]`.
Initially `dp[i+1][j+1] = 0`, `start[i+1][j+1] = -1`.
If `s[i] == t[j]`, `dp[i+1][j+1] = 1 + dp[i][j]`. `start[i+1][j+1] = j == 0 ? i : start[i][j]`.
If `s[i] != t[j]`, `dp[i+1][j+1] = max(dp[i][j+1], dp[i][j])`. If `dp[i+1][j+1] == dp[i][j+1]`, `start[i+1][j+1] = start[i][j + 1]`. Additionally, if `dp[i+1][j+1] = dp[i][j]`, `start[i+1][j+1] = max(start[i+1][j+1], start[i][j])`
```cpp
// OJ: https://leetcode.com/problems/minimum-window-subsequence
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
string minWindow(string s, string t) {
int M = s.size(), N = t.size();
vector<vector<int>> dp(M + 1, vector<int>(N + 1)), start(M + 1, vector<int>(N + 1, -1));
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (s[i] == t[j]) {
dp[i + 1][j + 1] = 1 + dp[i][j];
if (j == 0) {
start[i + 1][j + 1] = i;
} else {
start[i + 1][j + 1] = start[i][j];
}
} else {
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i][j]);
if (dp[i + 1][j + 1] == dp[i][j + 1]) {
start[i + 1][j + 1] = start[i][j + 1];
}
if (dp[i + 1][j + 1] == dp[i][j]) {
start[i + 1][j + 1] = max(start[i + 1][j + 1], start[i][j]);
}
}
}
}
int minLen = INT_MAX, st = -1;
for (int i = 0; i < M; ++i) {
if (dp[i + 1][N] == N && i - start[i + 1][N] + 1 < minLen) {
st = start[i + 1][N];
minLen = i - start[i + 1][N] + 1;
}
}
return minLen == INT_MAX ? "" : s.substr(st, minLen);
}
};
```
Since `dp[i + 1][j + 1]` only depends on `dp[i][j]` and `dp[i + 1][j]`, we can reduce the space complexity from `O(MN)` to `O(N)`.
```cpp
// OJ: https://leetcode.com/problems/minimum-window-subsequence
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(N)
class Solution {
public:
string minWindow(string s, string t) {
int M = s.size(), N = t.size(), minLen = INT_MAX, st = -1;
vector<int> dp(N + 1), start(N + 1, -1);
for (int i = 0; i < M; ++i) {
for (int j = N - 1; j >= 0; --j) {
if (s[i] == t[j]) {
dp[j + 1] = 1 + dp[j];
start[j + 1] = j == 0 ? i : start[j];
} else {
dp[j + 1] = max(dp[j + 1], dp[j]);
if (dp[j + 1] == dp[j]) start[j + 1] = max(start[j + 1], start[j]);
}
}
if (dp[N] == N && i - start[N] + 1 < minLen) {
st = start[N];
minLen = i - start[N] + 1;
}
}
return minLen == INT_MAX ? "" : s.substr(st, minLen);
}
};
```
## Solution 2. Greedy
<h4 id="intuition-2">Intuition</h4>
<p>Let <code>start</code> and <code>end</code> denote the left-most and right-most indices of a given window/substring in <code>s1</code>.</p>
<p>We can try all possible values of <code>start</code>, and for each of them, find the smallest value of <code>end</code> such that <code>s2</code> is a subsequence of <code>s1[start..end]</code>. Let <code>m</code> denote the length of <code>s2</code>. We want to find a sequence of indices <span class="math math-inline"><span class="katex"><span class="katex-mathml">i0,i1,...,im−1i_0, i_1, ..., i_{m-1}</span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 0.8679em; vertical-align: -0.2083em;"></span><span class="mord"><span class="mord mathnormal">i</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height: 0.3011em;"><span style="top: -2.55em; margin-left: 0em; margin-right: 0.05em;"><span class="pstrut" style="height: 2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">0</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height: 0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right: 0.1667em;"></span><span class="mord"><span class="mord mathnormal">i</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height: 0.3011em;"><span style="top: -2.55em; margin-left: 0em; margin-right: 0.05em;"><span class="pstrut" style="height: 2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">1</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height: 0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right: 0.1667em;"></span><span class="mord">...</span><span class="mpunct">,</span><span class="mspace" style="margin-right: 0.1667em;"></span><span class="mord"><span class="mord mathnormal">i</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height: 0.3011em;"><span style="top: -2.55em; margin-left: 0em; margin-right: 0.05em;"><span class="pstrut" style="height: 2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">m</span><span class="mbin mtight">−</span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height: 0.2083em;"><span></span></span></span></span></span></
没有合适的资源?快使用搜索试试~ 我知道了~
My C++ Code for LeetCode OJ.zip
共2000个文件
md:2000个
需积分: 5 0 下载量 139 浏览量
2023-12-31
11:12:58
上传
评论
收藏 11.34MB ZIP 举报
温馨提示
My C++ Code for LeetCode OJ
资源推荐
资源详情
资源评论
收起资源包目录
My C++ Code for LeetCode OJ.zip (2000个子文件)
README.md 98KB
README.md 66KB
README.md 32KB
README.md 18KB
README.md 17KB
README.md 14KB
README.md 12KB
README.md 12KB
README.md 11KB
README.md 11KB
README.md 11KB
README.md 11KB
README.md 11KB
README.md 10KB
README.md 10KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 8KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
暮苍梧~
- 粉丝: 41
- 资源: 258
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 3122080306 邹子轩 实验报告二.docx
- 基于STM32 NUCLEO板设计彩色LED照明灯(纯cubeMX开发)(大赛作品,文档完整,可直接运行)
- 发那科工业机器人保养大全
- Sphere.h
- REMD固有时间尺度分解信号分量可视化(Matlab完整源码和数据)
- 嵌入式系统双单片机STC89C52+STC15W104多功能学习板电路图可扩展 适用于单片机初学者和教学
- 基于STM32蓝牙控制小车系统设计(硬件+源代码+论文)大赛作品
- XILINXFPGA源码基于Spartan3火龙刀系列FPGA开发板VGA测试例程
- Java聊天室的设计与实现【尚学堂·百战程序员】
- python中matplotlib教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功