✅☑Beats 97% Users || [C++/Java/Python/JavaScript] || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n1. Problem Objective:\n\n    - The goal is to find the minimum win
# PLEASE UPVOTE IF IT HELPED\n\n---\n![Screenshot 2024-02-04 060439.png](https://assets.leetcode.com/users/images/4961d82d-008a-4631-8fca-dc172a848566_1707006901.7990885.png)\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n1. **Problem Objective:**\n\n    - The goal is to find the minimum window in string `s` that contains all the characters in string `t` in any order.\n1. **Sliding Window Technique:**\n\n    - Utilizes the sliding window technique where two pointers (`l` and `r`) represent the left and right ends of the current window.\n1. **Counting Characters:**\n\n    - `dictT` is used to count the occurrences of characters in string `t`.\n    - `windowCounts` keeps track of the characters within the current window.\n1. **Maintaining the Window:**\n\n    - Move the right pointer `r` to expand the window.\n    - Check if the characters in the current window satisfy the required counts from `t`.\n    - If satisfied, increment `formed` (count of characters satisfying the required counts).\n1. **Shrinking the Window:**\n\n    - Move the left pointer `l` to shrink the window.\n    - Update the minimum window size (`ans`) if a smaller valid window is found.\n    - Continue this process until a smaller valid window is not possible.\n    - Repeat until the right pointer `r` reaches the end of the string.\n\n\n\n# Complexity\n- Time complexity:\n   $$O(s + t)$$\n*(where s and t are the lengths of strings s and t.)*\n    \n\n- Space complexity:\n   $$O(s + t)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    string minWindow(string s, string t) {\n        if (s.empty() || t.empty()) {\n            return "";\n        }\n\n        unordered_map<char, int> dictT;\n        for (char c : t) {\n            int count = dictT[c];\n            dictT[c] = count + 1;\n        }\n\n        int required = dictT.size();\n        int l = 0, r = 0;\n        int formed = 0;\n\n        unordered_map<char, int> windowCounts;\n        int ans[3] = { -1, 0, 0 };\n\n        while (r < s.length()) {\n            char c = s[r];\n            int count = windowCounts[c];\n            windowCounts[c] = count + 1;\n\n            if (dictT.find(c) != dictT.end() && windowCounts[c] == dictT[c]) {\n                formed++;\n            }\n\n            while (l <= r && formed == required) {\n                c = s[l];\n\n                if (ans[0] == -1 || r - l + 1 < ans[0]) {\n                    ans[0] = r - l + 1;\n                    ans[1] = l;\n                    ans[2] = r;\n                }\n\n                windowCounts[c]--;\n                if (dictT.find(c) != dictT.end() && windowCounts[c] < dictT[c]) {\n                    formed--;\n                }\n\n                l++;\n            }\n\n            r++;\n        }\n\n        return ans[0] == -1 ? "" : s.substr(ans[1], ans[0]);\n    }\n};\n\n\n\n\n\n```\n```Java []\nclass Solution {\n    public String minWindow(String s, String t) {\n        if (s.isEmpty() || t.isEmpty()) {\n            return "";\n        }\n\n        Map<Character, Integer> dictT = new HashMap<>();\n        for (char c : t.toCharArray()) {\n            int count = dictT.getOrDefault(c, 0);\n            dictT.put(c, count + 1);\n        }\n\n        int required = dictT.size();\n        int l = 0, r = 0;\n        int formed = 0;\n\n        Map<Character, Integer> windowCounts = new HashMap<>();\n        int[] ans = { -1, 0, 0 };\n\n        while (r < s.length()) {\n            char c = s.charAt(r);\n            int count = windowCounts.getOrDefault(c, 0);\n            windowCounts.put(c, count + 1);\n\n            if (dictT.containsKey(c) && windowCounts.get(c).intValue() == dictT.get(c).intValue()) {\n                formed++;\n            }\n\n            while (l <= r && formed == required) {\n                c = s.charAt(l);\n\n                if (ans[0] == -1 || r - l + 1 < ans[0]) {\n                    ans[0] = r - l + 1;\n                    ans[1] = l;\n                    ans[2] = r;\n                }\n\n                windowCounts.put(c, windowCounts.get(c) - 1);\n                if (dictT.containsKey(c) && windowCounts.get(c).intValue() < dictT.get(c).intValue()) {\n                    formed--;\n                }\n\n                l++;\n            }\n\n            r++;\n        }\n\n        return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1);\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    def minWindow(self, s: str, t: str) -> str:\n        if not s or not t:\n            return ""\n\n        dictT = defaultdict(int)\n        for c in t:\n            dictT[c] += 1\n\n        required = len(dictT)\n        l, r = 0, 0\n        formed = 0\n\n        windowCounts = defaultdict(int)\n        ans = [-1, 0, 0]\n\n        while r < len(s):\n            c = s[r]\n            windowCounts[c] += 1\n\n            if c in dictT and windowCounts[c] == dictT[c]:\n                formed += 1\n\n            while l <= r and formed == required:\n                c = s[l]\n\n                if ans[0] == -1 or r - l + 1 < ans[0]:\n                    ans[0] = r - l + 1\n                    ans[1] = l\n                    ans[2] = r\n\n                windowCounts[c] -= 1\n                if c in dictT and windowCounts[c] < dictT[c]:\n                    formed -= 1\n\n                l += 1\n\n            r += 1\n\n        return "" if ans[0] == -1 else s[ans[1]:ans[2] + 1]\n\n\n```\n```javascript []\nvar minWindow = function (s, t) {\n    if (!s || !t) {\n        return "";\n    }\n\n    let dictT = new Map();\n    for (let c of t) {\n        dictT.set(c, (dictT.get(c) || 0) + 1);\n    }\n\n    let required = dictT.size;\n    let l = 0, r = 0;\n    let formed = 0;\n\n    let windowCounts = new Map();\n    let ans = [-1, 0, 0];\n\n    while (r < s.length) {\n        let c = s.charAt(r);\n        windowCounts.set(c, (windowCounts.get(c) || 0) + 1);\n\n        if (dictT.has(c) && windowCounts.get(c) === dictT.get(c)) {\n            formed++;\n        }\n\n        while (l <= r && formed === required) {\n            c = s.charAt(l);\n\n            if (ans[0] === -1 || r - l + 1 < ans[0]) {\n                ans[0] = r - l + 1;\n                ans[1] = l;\n                ans[2] = r;\n            }\n\n            windowCounts.set(c, windowCounts.get(c) - 1);\n            if (dictT.has(c) && windowCounts.get(c) < dictT.get(c)) {\n                formed--;\n            }\n\n            l++;\n        }\n\n        r++;\n    }\n\n    return ans[0] === -1 ? "" : s.substring(ans[1], ans[2] + 1);\n};\n\n\n\n```\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Hash Table', 'String', 'Sliding Window', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']