博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 727. Minimum Window Subsequence
阅读量:6930 次
发布时间:2019-06-27

本文共 1495 字,大约阅读时间需要 4 分钟。

Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of 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].

 

class Solution {public:    string minWindow(string s, string t) {        int ns = s.size(), nt= t.size();        int dp[ns+1][nt+1] = {};        const int mxx = ns + 1;        for (int i = 0 ; i <= ns; ++i) {            for (int j = 1; j <= nt; ++j) {                dp[i][j] = mxx;                if (i) {                    dp[i][j] = min(dp[i][j], 1 + dp[i-1][j]);                    if (s[i-1] == t[j-1]) dp[i][j]  = min(dp[i][j], 1 + dp[i-1][j-1]);                }            }        }                int ans = ns + 1, x = -1;        for (int i = 0; i <=ns; ++i) if (dp[i][nt] < ans) {            x = i;            ans = dp[i][nt];        }                if (x < 0) return "";        return s.substr(x-ans,ans);    }};

 

转载于:https://www.cnblogs.com/jxr041100/p/8429985.html

你可能感兴趣的文章
Grunt.js 初使用
查看>>
c语言中函数调用惯例
查看>>
[内存管理实践 之 1]在返回按钮中,释放内存
查看>>
android 加载大图片
查看>>
Configuration Manager 和内容位置(包源文件)
查看>>
防跨站脚本攻击的代码实例
查看>>
[物理学与PDEs]第1章第4节 电磁能量和电磁动量, 能量、动量守恒与转化定律 4.2 电磁动量, 动量守恒与转化定律...
查看>>
独领风骚:单例模式
查看>>
如花搞笑图片集锦(转贴)
查看>>
spring mvc DispatcherServlet详解之前传---FrameworkServlet
查看>>
Sql开发技巧
查看>>
TDictionary 是delphi用的,c++builder用起来太吃力。
查看>>
centos安装redis及php-redis扩展
查看>>
[DOM Event Learning] Section 4 事件分发和DOM事件流
查看>>
GBK、UTF8、UNICODE编码转换
查看>>
关于web页面性能测量指标与建议
查看>>
linux tar命令简介
查看>>
GTD时间管理(1)---捕获搜集
查看>>
分享web前端七款HTML5 Loading动画特效集锦
查看>>
HttpWebRequest和HttpWebResponse
查看>>