76. Minimum Window Substring 10-line template that can solve most 'substring' problems
来源:互联网 发布:淘宝一天最多几个好评 编辑:程序博客网 时间:2024/05/21 05:20
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 empty string ""
.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
The code of solving this problem is below. It might be the shortest among all solutions provided in Discuss.
string minWindow(string s, string t) { vector<int> map(128,0); for(auto c: t) map[c]++; int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0; while(end<s.size()){ if(map[s[end++]]-->0) counter--; //in t while(counter==0){ //valid if(end-begin<d) d=end-(head=begin); if(map[s[begin++]]++==0) counter++; //make it invalid } } return d==INT_MAX? "":s.substr(head, d); }
Here comes the template.
For most substring problem, we are given a string and need to find a substring of it which satisfy some restrictions. A general way is to use a hashmap assisted with two pointers. The template is given below.
int findSubstring(string s){ vector<int> map(128,0); int counter; // check whether the substring is valid int begin=0, end=0; //two pointers, one point to tail and one head int d; //the length of substring for() { /* initialize the hash map here */ } while(end<s.size()){ if(map[s[end++]]-- ?){ /* modify counter here */ } while(/* counter condition */){ /* update d here if finding minimum*/ //increase begin to make it invalid/valid again if(map[s[begin++]]++ ?){ /*modify counter here*/ } } /* update d here if finding maximum*/ } return d; }
One thing needs to be mentioned is that when asked to find maximum substring, we should update maximum after the inner while loop to guarantee that the substring is valid. On the other hand, when asked to find minimum substring, we should update minimum inside the inner while loop.
The code of solving Longest Substring with At Most Two Distinct Characters is below:
int lengthOfLongestSubstringTwoDistinct(string s) { vector<int> map(128, 0); int counter=0, begin=0, end=0, d=0; while(end<s.size()){ if(map[s[end++]]++==0) counter++; while(counter>2) if(map[s[begin++]]--==1) counter--; d=max(d, end-begin); } return d; }
The code of solving Longest Substring Without Repeating Characters is below:
Update 01.04.2016, thanks @weiyi3 for advise.
int lengthOfLongestSubstring(string s) { vector<int> map(128,0); int counter=0, begin=0, end=0, d=0; while(end<s.size()){ if(map[s[end++]]++>0) counter++; while(counter>0) if(map[s[begin++]]-->1) counter--; d=max(d, end-begin); //while valid, update d } return d; }
I think this post deserves some upvotes! : )
- 76. Minimum Window Substring 10-line template that can solve most 'substring' problems
- Here is a 10-line template that can solve most 'substring' problems
- Here is a 10-line template that can solve most 'substring' problems
- 76. Minimum Window Substring
- 76.Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 76. Minimum Window Substring
- 剑指offer--面试题7:重建二叉树
- css笔记:包含块(Containing Block)
- Java还要再学一遍基础(十五)
- 第一节 scala 变量的定义和使用
- 04_Qt的画笔、作画和自定义控件
- 76. Minimum Window Substring 10-line template that can solve most 'substring' problems
- <java EE 项目:petstore> 从一个简单项目看 java web 如何在本jsp页面上对用户输入的格式进行限制与验证
- CANopen 块传输通信详解
- JAVA WEB -事务处理
- java泛型之桥方法
- 关于Fragment的一些使用心得-跳转和返回栈实战浅析
- iOS 表格数据刷新
- 生活在智慧龙岗的居民究竟有多幸福?
- SpringMVC+Spring+Mybatis整合配置