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 ...
随机推荐
- DOM优化
一:DOM与浏览器: 重排:改变页面的内容. 重绘:浏览器显示的内容. 添加顺序:尽量在appendchild之前. 合并DOM操作-利用csstext, 缓存布局信息 文档碎片. 二 DOM 与事件 ...
- 【Selenium2+Python】常用操作
Webdriver中比较常用的操作元素的方法: clear() 清除输入框的默认内容 send_keys("xxx") 在一个输入框里输入xx内容 ——如果输入中文,则 ...
- HDU1215(筛选法)
题意:求n的所有因子和: 思路:类似于筛选法求素数的思想,只有第一次的时候了解过它的思想,然后就只是用来求素数,思想的运用反而少: 筛选法求素数: int prime() { memset(vis, ...
- 添加数据之后不跳页面显示一个漂亮的提示信息(非ajax提交数据)
1.在后台设置一个添加成功与否的提示 2.在添加页面设置提示信息 (自己喜欢什么样式就条成什么样式) 3.写js控制提示信息的显示与消失
- Neo4j 两种索引Legacy Index与Schema Index区别
Legacy Indexes 在Neo4j 2.0版本之前,Legacy index被称作indexes.这个索引是通过外部图存储在外的Lucene实现,允许“节点”和“联系”以key:value键值 ...
- Java 部分注意160530
1.1 变量的名字不可以重复 1.2 标识符命名规则:必须以字母_下划线货比富豪开头,余下的字符可以是下划线,货币符号,任何字母或数字,长度不限.不能用Java中的关键字或保留字做标识符. 1.3 l ...
- LeetCode【217. Contains Duplicate】
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- python requests库入门[转]
首先,确认一下: Requests 已安装 Requests是 最新的 让我们从一些简单的示例开始吧. 发送请求 使用Requests发送网络请求非常简单. 一开始要导入Requests模块: > ...
- lua遍历文件夹, zerobrane下载
参考的这个http://www.cnblogs.com/jiufangding/p/3931585.html,配合批处理. zerobrane下载(上一篇博客忘掉了): http://files.cn ...
- 单例设计模式getInstance()
对象的实例化方法,也是比较多的,最常用的方法是直接使用new,而这是最普通的,如果要考虑到其它的需要,如单实例模式,层次间调用等等. 直接使用new就不可以实现好的设计好,这时候需要使用间接使用n ...