Here is a 10-line template that can solve most 'substring' problems
来源:互联网 发布:淘宝静物拍摄相机设置 编辑:程序博客网 时间:2024/05/11 02:09
I will first give the solution then show you the magic template.
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! : )
- 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 10-line template that can solve most 'substring' problems
- 托福写作5-what is the most important characteristic that a person can have to be successful in life
- What are the problems that a NoSQL database tries to solve?
- question1.Describe a day that you enjoy the most or is the most special for you.
- problems that may cause a
- Expecting a string, date or number here, Expression x is instead a freemarker.template.TemplateBoole
- Expecting a string, date or number here, Expression x is instead a freemarker.template.TemplateBoole
- solve SetDefaultDllDirectories’ : is not a member of ‘`global namespace” from line 638 in atlcore.h
- You can Solve a Geometry Problem too
- hdu1086You can Solve a Geometry Problem too
- You can Solve a Geometry Problem too
- You can Solve a Geometry Problem too
- You can Solve a Geometry Problem too
- A - Can you solve this equation?
- Excel has detected that '...xls' is a SYLK file, but can not load it
- Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems [ILLUSTRATED]
- Linux环境内存泄漏工具vargrind
- PAT 乙级 1057. 数零壹(20)
- Distance Transform 距离变换
- ORACL资料整理
- 数据库设计三大范式的详解
- Here is a 10-line template that can solve most 'substring' problems
- 如何修改tomcat的默认端口为80
- android文件存储的4种方式
- 找出数组中不重复的数-Java
- UVa: 133
- makefile通配符
- *** error 65: access violation at 0x68014802 : no 'write' permission
- Subsets II
- 程序员面试金典: 9.9 递归和动态规划 9.2机器人走路的方式_避免禁区