Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

For example,
S = "ADOBECODEBANC"
T = "ABC"

Minimum window is "BANC".

Note:
If there is no such window in S that covers all characters in T, return the emtpy string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

采用双指针,end指针逐渐增加,当begin与end之间包含所有t后,后移begin,找到最小substr,并记录,最后输出最小substr。边界条件,begin处的字母属于t且在该区域中仅出现t中的次数。

 class Solution {
public:
string minWindow(string s, string t) {
int slen=s.size();
int tlen=t.size();
if(tlen==||slen<tlen) return "";
int needfind[]={};
int hasfind[]={};
for(int i=;i<tlen;i++)
{
needfind[t[i]]++;
}
int count=;
int begin=;
int end=;
int minwindowsizetmp=;
int minbegin=;
int minend=slen-;
int minwindowsize=INT_MAX;
for(count=;end<slen;end++)
{
if(needfind[s[end]]==)
continue;
hasfind[s[end]]++;
if(hasfind[s[end]]<=needfind[s[end]])
{ count++;
} if(count==tlen)
{
while(begin<end)
{
if(needfind[s[begin]]==)
{
begin++;
continue;
}
if(hasfind[s[begin]]>needfind[s[begin]])
{
hasfind[s[begin]]--;//若该行放到begin++下边,则会出现下述错误,谨记。
begin++; continue;
}
else
break;
}
minwindowsizetmp=end-begin+; if(minwindowsizetmp<minwindowsize)
{
minbegin=begin;
minend=end;
minwindowsize=minwindowsizetmp;
}
}
}
if(minwindowsize==INT_MAX)
return "";
return s.substr(minbegin,minwindowsize);
}
};
wrong answer:
Input:"ADOBECODEBANC", "ABC"
Output:"ANC"
Expected:"BANC"

Minimum Window Substring的更多相关文章

  1. 53. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  2. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

  3. LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram

    1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...

  4. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

  5. 【LeetCode】76. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  6. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

  7. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. [Leetcode][JAVA] Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  9. Java for LeetCode 076 Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  10. lintcode 中等题:minimum window substring 最小子串覆盖

    题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

随机推荐

  1. mac java 环境设置

    MAC下JDK1.6下载路径 http://support.apple.com/kb/DL1572 Mac OS的java版本问题和Eclipse中无法找到jdk源代码的问题解决办法 下载包含源代码j ...

  2. 浅谈Hex编码算法

    一.什么是Hex 将每一个字节表示的十六进制表示的内容,用字符串来显示. 二.作用 将不可见的,复杂的字节数组数据,转换为可显示的字符串数据 类似于Base64编码算法 区别:Base64将三个字节转 ...

  3. java网络---流

    网络操作很大一部分功能就是输入和输出数据. 简单归纳就是上传和下载文件.文件也是数据的一种载体. java对数据的操作归并为流. 所以对于数据流的操作定义2个基本类. java.io.OutputSt ...

  4. (网络层)IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)

    目录 IP协议首部格式地址解析协议 ARP逆向地址解析协议 RARP网际控制报文协议 ICMP网际组管理协议IGMP IP 数据报首部 IP数据报首部格式: 最高位在左边,记为0 bit:最低位在右边 ...

  5. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  6. Oracle SQL Developer连接报错(ORA-12505)

    Oracle SQL Developer连接报错(ORA-12505) 之前我的Oracle数据库出现问题,费大波周折终于弄好了,今天又创建了一个DBA管理员的连接方式出现问题,本人现在把解决方案分享 ...

  7. INFORMATICA 的部署实施 MTP&MTS

    软件开发的一般都有三个环境,开发环境,用户接受度测试环境,生产环境.我最近实施了从开发环境到生产环境的部署工作,在此跟大家分享一下. 大概步骤如下: 1 备份生产环境INFORMATICA 知识库  ...

  8. Effective Java 13 Minimize the accessibility of classes and members

    Information hiding is important for many reasons, most of which stem from the fact that it decouples ...

  9. 窗体DataGridView控件中按回车键时,单元格向下移动,如何能改成向右移动

    方法一:protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e) { base.OnKeyUp(e); if (e.Ke ...

  10. Github学习进阶-初露锋芒,通过命令行将本地git仓库推送到Github上面的仓库

    前提: 1. 需要安装git 客户端.  能打开 git  bash 命令行窗口. 2. 生成了ssh 秘钥,并添加到了Github上面. 一.在Github上面建立一个git仓库. 点击 + 号,在 ...