🔥🔥🔥 Easy Sliding Window Solution | Explained 🔥🔥🔥
Intuition\nThe main problem is finding the smallest substring in s that contains all the characters in t. Use the sliding window concept to solve this problem.\
# Intuition\nThe main problem is finding the **smallest** substring in **s** that contains all the characters in **t**. Use the sliding window concept to solve this problem.\n\n# Approach\n**Character Frequency t:** We start by counting the frequency of each character in **t** using a hashmap.\n\n**Sliding Window Expansion:** We use two pointers (**left** and **right**) to represent our current window in **s**. Initially, our window is empty. We expand our window by moving end to the right, thereby including more characters of s into our window.\n\n**Tracking Character Frequencies in the Window:** As we include more characters in our window, we use another hashmap to keep track of the frequency of each character within the window.\n\n**Valid Window Check:** Our goal is to adjust the window to include at least as many of each character as are found in t. Once our window includes enough of each required character, we have a valid window.\n\n**Window Minimization:**  reduce its size from the left, maintaining validity (is it still contains all characters from t) while seeking the minimum length. We do this by incrementally moving start to the right, reducing the window size until it no longer meets the criteria.\n\n**Result Update and Repeat:** Each time a valid minimum window is found, we compare its size to the smallest found so far and update our result accordingly. We continue this process until end has reached the end of s.\n\nTermination: If we find a valid window, we return it. If no such window exists, we return an empty string.\n\n# Complexity\n- Time complexity:\n**$$O(n)$$**\n\n- Space complexity:\n**$$O(n)$$**\n\n# Code\n```javascript []\nvar minWindow = function(s, t) {\n    if (s.length < t.length) return ""\n    const tCount = {}\n    for (let char of t){\n        tCount[char] = (tCount[char] || 0) + 1\n    }\n    let needCount = Object.keys(tCount).length\n    let haveCount = 0\n    let left = 0\n    let idxResultWindow = [0, Infinity] // Store the start and end of the minimum window\n    const windowCount = {}\n\n    for (let right = 0 ; right < s.length ; right++){\n        const char = s[right]\n        windowCount[char] = (windowCount[char] || 0) + 1\n\n        if (windowCount[char] === tCount[char]){\n            haveCount++\n        }\n\n        // when the current window satisfy the needs, try to shrink the window\n        while (haveCount === needCount){\n            // Update result if this window is smaller than the previous one\n            if (right - left < idxResultWindow[1] - idxResultWindow[0]){\n                idxResultWindow = [left, right]\n            }\n\n            // slide the left window pointer to minimize the solution\n            windowCount[s[left]]--\n            if (windowCount[s[left]] < tCount[s[left]]){\n                haveCount--\n            }\n            left++\n        }\n    }\n    console.log(idxResultWindow)\n    return idxResultWindow[1] === Infinity ? "" : s.slice(idxResultWindow[0], idxResultWindow[1] + 1)\n};\n```\n```python3 []\nfrom collections import defaultdict\n\nclass Solution:\n    def minWindow(self, s: str, t: str) -> str:\n        if len(s) < len(t):\n            return ""\n\n        t_count = defaultdict(int)\n        for char in t:\n            t_count[char] += 1\n\n        need_count = len(t_count)\n        have_count = 0\n        left = 0\n        idx_result_window = [0, float(\'inf\')]\n        window_count = defaultdict(int)\n\n        for right in range(len(s)):\n            char = s[right]\n            window_count[char] += 1\n\n            if window_count[char] == t_count[char]:\n                have_count += 1\n\n            while have_count == need_count:\n                if right - left < idx_result_window[1] - idx_result_window[0]:\n                    idx_result_window = [left, right]\n\n                window_count[s[left]] -= 1\n                if window_count[s[left]] < t_count[s[left]]:\n                    have_count -= 1\n\n                left += 1\n\n        return "" if idx_result_window[1] == float(\'inf\') else s[idx_result_window[0]:idx_result_window[1] + 1]\n\n```\n```Go []\nfunc minWindow(s string, t string) string {\n\tif len(s) < len(t) {\n\t\treturn ""\n\t}\n\n\ttCount := make(map[byte]int)\n\tfor i := 0; i < len(t); i++ {\n\t\ttCount[t[i]]++\n\t}\n\n\tneedCount := len(tCount)\n\thaveCount := 0\n\tleft := 0\n\tidxResultWindow := [2]int{0, math.MaxInt64}\n\twindowCount := make(map[byte]int)\n\n\tfor right := 0; right < len(s); right++ {\n\t\tchar := s[right]\n\t\twindowCount[char]++\n\n\t\tif windowCount[char] == tCount[char] {\n\t\t\thaveCount++\n\t\t}\n\n\t\tfor haveCount == needCount {\n\t\t\tif right-left < idxResultWindow[1]-idxResultWindow[0] {\n\t\t\t\tidxResultWindow = [2]int{left, right}\n\t\t\t}\n\n\t\t\twindowCount[s[left]]--\n\t\t\tif windowCount[s[left]] < tCount[s[left]] {\n\t\t\t\thaveCount--\n\t\t\t}\n\t\t\tleft++\n\t\t}\n\t}\n\n\tif idxResultWindow[1] == math.MaxInt64 {\n\t\treturn ""\n\t}\n\treturn s[idxResultWindow[0] : idxResultWindow[1]+1]\n}\n```\n```Java []\nclass Solution {\n    public String minWindow(String s, String t) {\n        if (s.length() < t.length()) return "";\n        HashMap<Character, Integer> tCount = new HashMap<>();\n        for (char c : t.toCharArray()) {\n            tCount.put(c, tCount.getOrDefault(c, 0) + 1);\n        }\n        int needCount = tCount.size();\n        int haveCount = 0;\n        int left = 0;\n        int[] idxResultWindow = {0, Integer.MAX_VALUE};\n        HashMap<Character, Integer> windowCount = new HashMap<>();\n\n        for (int right = 0; right < s.length(); right++) {\n            char c = s.charAt(right);\n            windowCount.put(c, windowCount.getOrDefault(c, 0) + 1);\n\n            if (windowCount.get(c).equals(tCount.get(c))) {\n                haveCount++;\n            }\n\n            while (haveCount == needCount) {\n                if (right - left < idxResultWindow[1] - idxResultWindow[0]) {\n                    idxResultWindow[0] = left;\n                    idxResultWindow[1] = right;\n                }\n\n                windowCount.put(s.charAt(left), windowCount.get(s.charAt(left)) - 1);\n                if (windowCount.get(s.charAt(left)) < tCount.getOrDefault(s.charAt(left), 0)) {\n                    haveCount--;\n                }\n                left++;\n            }\n        }\n\n        return idxResultWindow[1] == Integer.MAX_VALUE ? "" : s.substring(idxResultWindow[0], idxResultWindow[1] + 1);\n    }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    std::string minWindow(std::string s, std::string t) {\n        if (s.length() < t.length()) return "";\n        \n        std::unordered_map<char, int> tCount;\n        for (char c : t) {\n            tCount[c]++;\n        }\n\n        int needCount = tCount.size();\n        int haveCount = 0;\n        int left = 0;\n        std::pair<int, int> idxResultWindow = {0, INT_MAX};\n        std::unordered_map<char, int> windowCount;\n\n        for (int right = 0; right < s.length(); right++) {\n            char currentChar = s[right];\n            windowCount[currentChar]++;\n\n            if (windowCount[currentChar] == tCount[currentChar]) {\n                haveCount++;\n            }\n\n            while (haveCount == needCount) {\n                if (right - left < idxResultWindow.second - idxResultWindow.first) {\n                    idxResultWindow = {left, right};\n                }\n\n                // Slide the left window pointer to minimize the solution\n                windowCount[s[left]]--;\n                if (windowCount[s[left]] < tCount[s[left]]) {\n                    haveCount--;\n                }\n                left++;\n            }\n        }\n\n        std::cout << "[" << idxResultWindow.first << ", " << idxResultWindow.second << "]" << std::endl;\n\n        return idxResultWindow.second == INT_MAX ? "" : s.substr(idxResultWindow.first, idxResultWindow.second - idxResultWindow.first + 1);\n    }\n};\n```\n# PLEASE UPVOTE IF ITS HELP!!!!!\uD83D\uDE4F\uD83D\uDE4F\uD83D\uDE4F\uD83D\uDE09
['Hash Table', 'String', 'Sliding Window', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript']