【LeetCode】76. Minimum Window Substring
Minimum Window Substring
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.
由于大小写字母的ASCII码不大于128,因此开辟两个数组存储信息。
needFind数组存储T字符串每个字符出现次数。例如:needFind['A']=5意为T中A出现5次。
Found数组存储S字符串在[begin,end]窗口内每个字符出现次数。
算法核心思想如下:
在保证[begin,end]窗口包含T中所有字符的条件下,延伸end,收缩begin。
进行一次扫描后,记录符合条件的最小窗口(end-begin+1)表示的字符串。
有个问题:怎样才知道[begin,end]窗口包含了T中所有字符?
我使用count记录剩余“有效”字符数,当count达到0时,即可说明[begin,end]包含了T。
注意:“有效”的意思是指,当前延伸得到的S[end]字符,使得[begin,end]更进一步包含T,而不是重复劳动。
比如说,T="a", [begin,end]已经包含"a",再延伸得到"aa",只是无效操作,并没有使得[begin,end]更接近T,有效字符数仍为1.
class Solution {
public:
string minWindow(string S, string T) {
int begin = ;
int end = ;
int minbegin = ;
int minend = ;
int minSize = INT_MAX;
vector<int> needFind(, );
vector<int> Found(, );
for(int i = ; i < T.size(); i ++)
needFind[T[i]] ++;
Found[S[]] ++;
int count = T.size();
if(needFind[S[]] >= Found[S[]])
count --;
while(true)
{
if(count == )
{//shrink begin
while(Found[S[begin]] > needFind[S[begin]])
{
Found[S[begin]] --;
begin ++;
}
int size = end-begin+;
if(size < minSize)
{
minbegin = begin;
minend = end;
minSize = size;
}
}
if(end < S.size())
{
end ++;
Found[S[end]] ++;
if(needFind[S[end]] >= Found[S[end]])
count --;
}
else
break;
}
if(minSize != INT_MAX)
return S.substr(minbegin, minSize);
else
return "";
}
};
【LeetCode】76. Minimum Window Substring的更多相关文章
- 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...
- 【一天一道LeetCode】#76. Minimum Window Substring
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 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 ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
随机推荐
- Shell基础学习(三) 传递参数
我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 以下实例我们向脚本传递三个参数 ...
- PHP 基础函数(二)数组的内部指针
current($arr); 返回数组中的当前单元pos($arr); 返回数组中的当前单元key($arr); 返回数组中当前单元的键名prev($arr); 将数组中的内部指针倒回一位ne ...
- layoutit note
Head: JavaScript -> Navbar Menu: JavaScript -> Collapse Compnents -> Panels Compnents -> ...
- ADC Power Supplies
http://www.planetanalog.com/author.asp?section_id=3041&doc_id=563055 Jonathan Harris, Product Ap ...
- WM-G-MR-09模块
WM-G-MR-09模块,该模块同时支持SDIO与SPI 模式 USI(环隆电气)WM-G-MR-09,该WiFi芯片支持802.11b/g无线网络模式,芯片体积8.2×8.4×1.35(mm),采用 ...
- Windows Embedded Compact 7网络编程概述(上)
如今,不论是嵌入式设备.PDA还是智能手机,网络都是必不可少的模块.网络使人们更方便地共享设备上的信息和资源.而且,利用智能手机浏览互联网,也逐渐成为生活中的常见手段.物联网所倡导的物物相联,也离不开 ...
- hdu4035之经典慨率DP
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submi ...
- Appium+python自动化10-AVD 模拟器
前言 有些小伙伴没android手机,这时候可以在电脑上开个模拟器玩玩 一.模拟器配置 1.双击启动AVD Manager,进入配置界面
- IDE的文件和代码模板
设置IDE的的模板,可以在创建文件的时候,自动产生模板内容,模板里可以 模板头设置: # -*- coding: utf- -*- """ --------------- ...
- flume学习(三):flume将log4j日志数据写入到hdfs(转)
原文链接:flume学习(三):flume将log4j日志数据写入到hdfs 在第一篇文章中我们是将log4j的日志输出到了agent的日志文件当中.配置文件如下: tier1.sources=sou ...