Here is a 10-line template that can solve most 'substring' problems
I will first give the solution then show you the magic template.\n\nThe code of solving this problem is below. It might be the shortest among all solutions prov
I will first give the solution then show you the magic template.\n\n**The code of solving this problem is below. It might be the shortest among all solutions provided in Discuss**.\n\n    string minWindow(string s, string t) {\n            vector<int> map(128,0);\n            for(auto c: t) map[c]++;\n            int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0;\n            while(end<s.size()){\n                if(map[s[end++]]-->0) counter--; //in t\n                while(counter==0){ //valid\n                    if(end-begin<d)  d=end-(head=begin);\n                    if(map[s[begin++]]++==0) counter++;  //make it invalid\n                }  \n            }\n            return d==INT_MAX? "":s.substr(head, d);\n        }\n\n**Here comes the template.**\n\nFor 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.\n \n\n    int findSubstring(string s){\n            vector<int> map(128,0);\n            int counter; // check whether the substring is valid\n            int begin=0, end=0; //two pointers, one point to tail and one  head\n            int d; //the length of substring\n\n            for() { /* initialize the hash map here */ }\n    \n            while(end<s.size()){\n\n                if(map[s[end++]]-- ?){  /* modify counter here */ }\n    \n                while(/* counter condition */){ \n                     \n                     /* update d here if finding minimum*/\n\n                    //increase begin to make it invalid/valid again\n                    \n                    if(map[s[begin++]]++ ?){ /*modify counter here*/ }\n                }  \n  \n                /* update d here if finding maximum*/\n            }\n            return d;\n      }\n\n*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.*\n\n\nThe code of solving **Longest Substring with At Most Two Distinct Characters** is below:\n\n    int lengthOfLongestSubstringTwoDistinct(string s) {\n            vector<int> map(128, 0);\n            int counter=0, begin=0, end=0, d=0; \n            while(end<s.size()){\n                if(map[s[end++]]++==0) counter++;\n                while(counter>2) if(map[s[begin++]]--==1) counter--;\n                d=max(d, end-begin);\n            }\n            return d;\n        }\n\nThe code of solving **Longest Substring Without Repeating Characters** is below:\n\n**Update 01.04.2016, thanks @weiyi3 for advise.**\n\n    int lengthOfLongestSubstring(string s) {\n            vector<int> map(128,0);\n            int counter=0, begin=0, end=0, d=0; \n            while(end<s.size()){\n                if(map[s[end++]]++>0) counter++; \n                while(counter>0) if(map[s[begin++]]-->1) counter--;\n                d=max(d, end-begin); //while valid, update d\n            }\n            return d;\n        }\n    \nI think this post deserves some upvotes! : )
['String']