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. Java从零开始学四十三(DOM4j解析XML)

    一.创建XML // 建立XML public static void gernatorXML() { // 创建Document对象 Document doc = DocumentHelper.cr ...

  2. Spring概述

    layout: post title: Spring概述 tags: [Java,Spring,IOC,AOP] --- Spring是一个开源的轻量级Java SE(Java 标准版本)/Java ...

  3. OC语言-05-OC语言-内存管理

    一.引用计数器 1> 栈和堆 栈 ① 主要存储局部变量 ② 内存自动回收 堆 ① 主要存储需要动态分配内存的变量 ② 需要手动回收内存,是OC内存管理的对象 2> 简介 作用 ① 表示对象 ...

  4. Android中有时候运行程序的时候会报错:An internal error occurred during:。。。。

    解决办法: Project -> Properties -> Run/Debug Settings: 1. select "Launching New_configuration ...

  5. quote、cite、refer的区别

    quote指“直引”,直接引用原文,不做丝毫修改. cite指“间引”,引用时不需要相同的词. refer指消化原来的思想,完全不抄.

  6. 百度地图API说明

    JZ's Blog的博客对百度地图说明很清晰 http://www.jiazhengblog.com/blog/2011/07/02/289/

  7. CListCtrl中删除多个不连续的行

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  8. cocos2d-x之Vector与map

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...

  9. JQ工具函数

    在jQuery中,工具函数是指直接依附于jQuery对象,针对jQuery对象本身定义的方法,即全局性的,我们统称为工具函数,或Utilites函数 主要作用于:字符串.数组.对象 API:工具函数 ...

  10. C语言杂谈(三)存储类别

    本文讨论C语言中的存储类别,包括数据在内存的存储.变量的存储类别.函数的存储类别.生存周期.下图为计算机的存储空间,有寄存器和内存. 一.存储区域 1.寄存器:存放立即参加运算的数据. 2.系统区:存 ...