C++ - 2 approaches for hashmap - way too much commenting and explanation
The idea of sliding window is this:\n1. Start two pointers - begin and end, both at 0.\n2. Keep begin constant. Move end (i.e., do end++) till the condition is 
**The idea of sliding window is this:**\n1. Start two pointers - begin and end, both at 0.\n2. Keep begin constant. Move end (i.e., do end++) till the condition is satisfied.\n3. Once the condition is satisfied, move the begin pointer (do begin++) for as long as the condition is still satisfied. Update the minLength and keep a track of where the start pointer is at for that particular length. If its smaller than a previous length, update it. If not, continue with resizing the window as below:\n4. Move end pointer to the right till the condition is satisfied again.\n5. Once the entire string is traversed, use the minStart pointer and minlength to get the substring.\n\n**Checking the condition:**\n* Now, the condition to satisfy here is - all chars (including duplicates) of string t are in the substring of s that we are processing. How do we check this? We need something like a hashcode. So build a hashtable/hashmap for t with it\'s chars and their respective frequencies. \n* Okay, now we need something similar for the substring we are processing so we can compare them both. Two ways to do this - create a plain vanilla hashmap for the substring or, use the map of t and use a counter to indicate number of chars to be matched. The second one is troubling for me. Its a bit too much to wrap my head around. nevertheless, code for both approaches are below:\n\n**WITH TWO HASHMAPS**\n```\nclass Solution {\nprivate:\n    bool isMatchingHashCode(map<char,int>& smap,map<char,int>& tmap){\n        for(auto it = tmap.begin(); it != tmap.end(); ++it){\n            char c= it->first; int freq = it->second;\n            if(smap[c] <freq)\n                return false;\n        }\n        return true;\n    }\n    \npublic:\n    string minWindow(string s, string t) {\n        \n        map<char,int> tmap,smap;\n        for(char c : t) tmap[c]++;\n        //tmap is the hashcode\n        \n        int start=0,end=0,counter=t.size(),minStart=0,minLen=INT_MAX;\n        // counter represents the number of chars of t to be found in s.\n        int size = s.size();\n        \n        while(end<size){\n            smap[s[end]]++;; //add the freq of the char to smap \n            end++; //window to the right\n            \n            //by this time, if we have reached the matching substring, we will start resizing. If not, then the window has been expanded on the right size and its proceeding\n            while(isMatchingHashCode(smap,tmap)){\n                if(end-start<minLen){\n                    minStart=start;\n                    minLen = end-start;\n                } //update the head and tail and length if needed, else, continue resizing\n                smap[s[start]]--; //going left \n                start++;\n            }\n            \n        }\n        \n        if(minLen!=INT_MAX){\n            return s.substr(minStart,minLen);\n        }\n        return "";\n        \n    }\n};\n```\n\nThis one is super slow. Comparing the two maps over and over takes time. \nHere is the second approach:\n\n**USING ONE HASHMAP**\n(Though i do understand this now, i dont think this comes to me intuitively. How does one develop this intuition? Please help me understand this. Thanks in advance!)\n```\nclass Solution {\npublic:\n    string minWindow(string s, string t) {\n        vector<int> tfreq(128,0);\n        for(char c : t) tfreq[c]++;\n        //tfreq is the hashcode\n        \n        int start=0,end=0,counter=t.size(),minStart=0,minLen=INT_MAX;\n        // counter represents the number of chars of t to be found in s.\n        int size = s.size();\n        \n        while(end<size){\n            //if character in s exists in t => i.e., if char exists in t, the freq would be positive. (0if non-existent, -ve never happens)\n            if(tfreq[s[end]]>0){\n                counter--; //no.of chars to be found is 1 less now\n            }\n            tfreq[s[end]]--; //reduce the freq of that char regardless of its presence in t\n            //if the char is not in t, it will result in negative frequency. later as we move the start pointer to the left, we will increase the freq of this char. So we will essentially cancel out all the subtractions we do on the char \n            end++; //window to the right\n            \n            //by this time, if we have reached the matching substring, we will start resizing. If not, then the window has been expanded on the right size and its proceeding\n            //when counter=0, tfreq should have all 0s\n            while(counter==0){\n                if(end-start<minLen){\n                    minStart=start;\n                    minLen = end-start;\n                } //update the head and tail and length if needed, and then continue resizing\n                tfreq[s[start]]++; \n                /*if the char existed in t already, then freq would be greater than 0. Otherwise it will be 0 or less than 0as we reduced the frequency of the char earlier regardless of it being in t or not*/\n                if(tfreq[s[start]]>0) counter++;\n                start++;\n            }\n            \n        }\n        if(minLen!=INT_MAX){\n            return s.substr(minStart,minLen);\n        }\n        return "";\n        \n    }\n};\n```
['C', 'C++']