✔️ ✔️Beat 97.97%|| Sliding Window Approach with Clear Comments and Explanation
Please upvote if you find the solution helpful.\n\n\n## Explanation\n\n1. Initialization:\n   - Create a character frequency map mp1 for string t.\n   - Initial
Please **upvote** if you find the solution helpful.\n![image](https://assets.leetcode.com/users/images/8de3a02c-f6c5-401a-8347-abafc58acb70_1707009383.8935752.png)\n\n## Explanation\n\n1. **Initialization**:\n   - Create a character frequency map `mp1` for string `t`.\n   - Initialize sliding window pointers `i` and `j`, and counters `count`, `mini`, and `start`.\n\n2. **Sliding Window Approach**:\n   - Move the end pointer `j` through string `s`.\n   - Adjust character counts and window size based on character occurrences.\n   - Shrink the window when all characters from `t` are found.\n\n3. **Return Minimum Window**:\n   - Return the minimum window substring of `s` if found, else return an empty string.\n\nThe algorithm efficiently identifies the minimum window in `s` containing all characters of `t`.\n\n**Time Complexity: O(S + T)**\n**Space Complexity: O(T)**\n**C++**\n```\n **Here\'s the code with concise comments:**\n\n```c++\nclass Solution {\npublic:\n  string minWindow(string s, string t) {\n    // Store character frequencies in t\n    unordered_map<char, int> mp1;\n    for (char c : t) {\n      mp1[c]++;\n    }\n\n    // Sliding window variables\n    int i = 0, j = 0, count = t.size(), mini = s.size() + 1, start = 0;\n\n    while (j < s.size()) {\n      // Decrement count if a required character is found\n      if (mp1.count(s[j]) && mp1[s[j]] > 0) {\n        count--;\n      }\n      mp1[s[j]]--; // Update character count in map\n\n      // Shrink window while all characters in t are found\n      while (count == 0) {\n        if (mini > j - i + 1) {\n          mini = j - i + 1;\n          start = i;\n        }\n        mp1[s[i]]++; // Re-adjust character count\n        if (mp1[s[i]] > 0) { // If a required character is removed, increment count\n          count++;\n        }\n        i++; // Move start pointer\n      }\n      j++; // Move end pointer\n    }\n\n    return mini == s.size() + 1 ? "" : s.substr(start, mini);\n  }\n};\n```\n**Java**\n```\nclass Solution {\n    public String minWindow(String s, String t) {\n        HashMap<Character, Integer> mp1 = new HashMap<>();\n        int count = t.length();\n        for (char i : t.toCharArray()) {\n            mp1.put(i, mp1.getOrDefault(i, 0) + 1);\n        }\n        int i = 0, j = 0, mini = s.length() + 1, start = 0;\n        while (j < s.length()) {\n            if (mp1.containsKey(s.charAt(j)) && mp1.get(s.charAt(j)) > 0) {\n                count--;\n            }\n            if (mp1.containsKey(s.charAt(j))) {\n                mp1.put(s.charAt(j), mp1.get(s.charAt(j)) - 1);\n            }\n            while (count == 0) {\n                if (mini > j - i + 1) {\n                    mini = j - i + 1;\n                    start = i;\n                }\n                if (mp1.containsKey(s.charAt(i))) {\n                    mp1.put(s.charAt(i), mp1.get(s.charAt(i)) + 1);\n                    if (mp1.get(s.charAt(i)) > 0) {\n                        count++;\n                    }\n                }\n                i++;\n            }\n            j++;\n        }\n        if (mini == s.length() + 1) {\n            return "";\n        }\n        return s.substring(start, start + mini);\n    }\n}\n```\n**python**\n```\nclass Solution(object):\n    def minWindow(self, s, t):\n        mp1 = defaultdict(int)\n        count = len(t)\n        for i in t:\n            mp1[i] += 1\n\n        i, j, mini, start = 0, 0, len(s) + 1, 0\n        while j < len(s):\n            if s[j] in mp1 and mp1[s[j]] > 0:\n                count -= 1\n            if s[j] in mp1:\n                mp1[s[j]] -= 1\n\n            while count == 0:\n                if mini > j - i + 1:\n                    mini = j - i + 1\n                    start = i\n                if s[i] in mp1:\n                    mp1[s[i]] += 1\n                    if mp1[s[i]] > 0:\n                        count += 1\n                i += 1\n            j += 1\n\n        if mini == len(s) + 1:\n            return ""\n        return s[start:start + mini]\n       \n```\n![image](https://assets.leetcode.com/users/images/356929c7-5780-45f4-8eb5-3299da5725d8_1707009629.3697278.jpeg)\n\n
['C', 'Sliding Window', 'Python', 'Java']