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的更多相关文章

  1. Minimum Window Substring @LeetCode

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

  2. 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 ...

  3. leetcode76. Minimum Window Substring

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

  4. 【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 ...

  5. 刷题76. Minimum Window Substring

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

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

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

  7. [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 ...

  8. 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 ...

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

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

随机推荐

  1. POJ 2184 01背包+负数处理

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10200   Accepted: 3977 D ...

  2. UIRefreshControl自动刷新

    不知道UIRefreshController是什么的朋友可以参考iOS6新特征:UIRefreshControl[下拉刷新]使用示例 一文了解这是什么,这里只提怎么使用代码的方式触发UIRefresh ...

  3. JDBC中的PreparedStatement

    PreparedStatement类从Statement中继承来. 可以将SQL语句传给数据库做编译处理,即在执行的SQL语句中包含一个或多个IN参数,可以设置IN参数值多次执行SQL语句,不必重新给 ...

  4. html、css、js注释

    HTML注释 <!--注释的内容--> CSS注释 /* 注释内容 */ JS注释 单行注释以 // 开头. 多行注释以 /* 开始,以 */ 结尾.

  5. 第二个Sprint冲刺团队贡献分

    201306114322 邵家文 50分 201306114319 陈俊金 10分 201306114320 李新    10分 201306114324 朱浩龙 10分

  6. 使用notepad++进行格式转换

    由于历史原因,导致Windows.Unix/Linux.Mac三者之间,对于文本中所用回车换行符,表示的方法,都不一样.这就导致了很多人都会遇到回车换行符的困惑,同时需要在不同格式间进行转换. 1)查 ...

  7. js窗口边缘滑入滑出效果-初级代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. [翻译]Understanding Weak References(理解弱引用)

    原文 Understanding Weak References Posted by enicholas on May 4, 2006 at 5:06 PM PDT 译文 我面试的这几个人怎么这么渣啊 ...

  9. android之ActionBar

    最近忙着做项目了,很久么来博客园看看了.最近项目中用到了actionbar,那就依我个人之建,来跟大家谈谈吧. 首先最重的是看你自己所见的项目的最小Api是为11,(在设置Minsdkversion最 ...

  10. 2016HUAS_ACM暑假集训3B - Frogger

    好几天没更新博客了,因为这周在看关于图论的算法,有好几个(还是英文名字-_-||),人晕晕的...... 说一下这个Frogger吧.这个题目的话......难的不是做法,而是题意... 大致题意:有 ...