53. 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.
思路:涉及到2点: 1. Hash, 保证查找为 O(1). 2. S 中设置两指针,根据长度确定右边指针位置;根据若去掉该字符,则该字符在 window 中出现次数将小于在 T 中出现的次数确定左边指针位置。
class Solution {
public:
string minWindow(string S, string T) {
if(T == "" || S == "") return "";
string s;
int ch[2]['z'+1] = {0};
for(int i = 0; i < T.size(); ++i) ++ch[0][T[i]];
int first = 0, cnt = 0;
for(int second = 0; second < S.size(); ++second) {
if(ch[0][S[second]]) {
if(ch[0][S[second]] > ch[1][S[second]])
++cnt;
++ch[1][S[second]];
}
if(cnt == T.size()) {
while(ch[0][S[first]] == 0 || ch[1][S[first]] > ch[0][S[first]]) {
if(ch[1][S[first]] > ch[0][S[first]])
ch[1][S[first]]--;
++first;
}
string tem = S.substr(first, second-first+1);
if(s == "" || s.size() > tem.size()) s = tem;
--ch[1][S[first++]];
--cnt;
}
}
return s;
}
};
53. Minimum Window Substring的更多相关文章
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- 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 ...
- leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...
- 【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 ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [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 ...
- 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 ...
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
随机推荐
- QT快速使用ntohs
QT快速使用ntohs,需要注意3点:1. ntohs只是转换相邻的2个字节2. 引入头文件#include <windows.h>3. 需要加上win32{LIBS+=-lws2_32} ...
- vertical-align及IE7下的inline-block
在IE7下,是不支持inline-block元素的,当对块级元素如dl进行inline-block样式设置时,在IE7浏览器是下样式是不会生效的. 若要在IE7下实现将块级元素设置为内联元素,可以这样 ...
- [UML][转]UML类图符号 各种关系说明以及举例
修改自:http://www.cnblogs.com/duanxz/archive/2012/06/13/2547801.html UML中描述类和类之间相互关系的方式包括:依赖(Dependency ...
- SQLite简单使用说明
System.Data.SQLite.dll下载地址 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 选择. ...
- 2014年5月份第1周51Aspx源码发布详情
郑州某高校学生考评系统源码 2014-5-5 [VS2008]功能介绍: 1.用户角色有部主任.教师.学生等. 2.可添加班级考评项目.学生考评项目. 3.可指定学生对班级.学生某考评项 ...
- HDU 4717 The Moving Points (三分法)
题意:给n个点的坐标的移动方向及速度,问在之后的时间的所有点的最大距离的最小值是多少. 思路:三分.两点距离是下凹函数,它们的max也是下凹函数.可以三分. #include<iostream& ...
- mac 下配置protobuf 3.0 golang环境
protobuf 3.0 与 之前的 protobuf 2.6 的语法是不一样的.需要重新安装一下,本机的环境是 OS X Yosemite 10.10.2 1. 不采用home brew安装,用 ...
- hasLayout与BFC的触发条件
hasLayout与BFC是分别在IE和其他浏览器上的两个作用很相近的概念,在很多时候,我们需要触发它们去实现有些效果.例如清除浮动时需要触发hasLayout与BFC:很多自适应的两栏和三栏布局(两 ...
- iOS view 颜色渐变
//渐变色过渡自然 CAGradientLayer *layer = [CAGradientLayer layer]; layer.frame = CGRectMake(0, 0, curW-10,4 ...
- [Java Basics2] Iterable, Socket, Reflection, Proxy, Factory DP
Parent interface of Collection: Iterable Interface A class that implements the Iterable can be used ...