Sliding window C++ solution
Using a sliding window to increase the right boundary to include more elements. Once the sliding window includes all elements in the target array, try to increa
Using a sliding window to increase the right boundary to include more elements. Once the sliding window includes all elements in the target array, try to increase the left boundary to decrease the size of the window until the window doesn\'t include all elements in the target array. Repeat the previous steps till the right boundary of the window reaches the end of the original array.\n\nThe mistake I had in my submission:\n1. when updating the minimum length of the window, I used right - left + 1, but actually I increased right before when updating the right boundary of the window.\n2. when updating the left boundary, I used valid ==  t.size(). I forgot the target array could have repeated elements. I should use need.size(), which is the size of the map. All keys are unique.\n\n```\nclass Solution {\npublic:\n    string minWindow(string s, string t) {\n        unordered_map<char, int> need, window;\n        \n        for(char c : t)\n            need[c]++;\n        \n        int left = 0, right = 0, valid = 0;\n        int start = 0, len = INT_MAX;\n        \n        while(right < s.size())\n        {\n            char c = s[right];\n            right++;\n            \n            if(need.count(c))\n            {\n                window[c]++;\n                if(window[c] == need[c])\n                    valid++;\n            }\n            \n            while(valid == need.size())\n            {\n                if(right - left < len)\n                {\n                    start = left;\n                    len = right - left;\n                }\n                \n                char c = s[left];\n                \n                if(need.count(c))\n                {\n                    if(window[c] == need[c])\n                        valid--;\n                    window[c]--;\n                }\n                left++;\n            }\n        }\n        \n        return len == INT_MAX ? "" : s.substr(start, len);\n    }\n};\n```
['C', 'Sliding Window']