🚀🚀🚀 BEATS 100% || EXPLAiNED ✅✅✅ || ANY LANGUAGE ✅✅✅ || 🚀🚀🚀 by PRODONiK
\n\n# Intuition\nThe goal is to find the minimum window in string s that contains all characters from string t. The intuition is to use a sliding window approac
![image.png](https://assets.leetcode.com/users/images/ece090fc-bfe3-4c72-92fe-b15fdd757704_1707004998.8177435.png)\n\n# Intuition\nThe goal is to find the minimum window in string `s` that contains all characters from string `t`. The intuition is to use a sliding window approach with two pointers.\n\n# Approach\n- Initialize a character array `map` of size 128 to store the frequency of characters in string `t`.\n- Initialize variables `count`, `start`, `end`, `minLen`, and `startIndex`.\n- Iterate through each character in string `t` and update the character frequency in the `map`.\n- Use two pointers (`start` and `end`) to slide the window and find the minimum window that contains all characters from string `t`.\n  - Increment `end` and decrease the frequency in `map` for each character encountered until all characters from `t` are present in the window.\n  - When all characters from `t` are present, update `minLen` and `startIndex` based on the current window size and starting index.\n  - Increment `start` and increase the frequency in `map` until the window no longer contains all characters from `t`.\n- After the iteration, the minimum window is found, and the result is a substring of `s` starting from `startIndex` with length `minLen`.\n\n# Complexity\n- Time complexity: $$O(n)$$, where n is the length of string `s`.\n- Space complexity: $$O(1)$$, as the `map` array has a constant size (128).\n\n![image.png](https://assets.leetcode.com/users/images/e44a9cd8-2c23-409f-9442-275de425b660_1707006416.6976166.png)\n\n# UPVOTE iF iT WAS HELPFUL :)\n# Code\n```java []\nclass Solution {\n    public String minWindow(String s, String t) {\n        if (s == null || t == null || s.length() == 0 || t.length() == 0 ||\n                s.length() < t.length()) {\n            return new String();\n        }\n        int[] map = new int[128];\n        int count = t.length();\n        int start = 0, end = 0, minLen = Integer.MAX_VALUE, startIndex = 0;\n        /// UPVOTE !\n        for (char c : t.toCharArray()) {\n            map[c]++;\n        }\n\n        char[] chS = s.toCharArray();\n\n        while (end < chS.length) {\n            if (map[chS[end++]]-- > 0) {\n                count--;\n            }\n            while (count == 0) {\n                if (end - start < minLen) {\n                    startIndex = start;\n                    minLen = end - start;\n                }\n                if (map[chS[start++]]++ == 0) {\n                    count++;\n                }\n            }\n        }\n\n        return minLen == Integer.MAX_VALUE ? new String() :\n                new String(chS, startIndex, minLen);\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    std::string minWindow(std::string s, std::string t) {\n        if (s.empty() || t.empty() || s.length() < t.length()) {\n            return "";\n        }\n\n        std::vector<int> map(128, 0);\n        int count = t.length();\n        int start = 0, end = 0, minLen = INT_MAX, startIndex = 0;\n        /// UPVOTE !\n        for (char c : t) {\n            map[c]++;\n        }\n\n        while (end < s.length()) {\n            if (map[s[end++]]-- > 0) {\n                count--;\n            }\n\n            while (count == 0) {\n                if (end - start < minLen) {\n                    startIndex = start;\n                    minLen = end - start;\n                }\n\n                if (map[s[start++]]++ == 0) {\n                    count++;\n                }\n            }\n        }\n\n        return minLen == INT_MAX ? "" : s.substr(startIndex, minLen);\n    }\n};\n```\n```Python []\nclass Solution:\n    def minWindow(self, s: str, t: str) -> str:\n        if not s or not t or len(s) < len(t):\n            return ""\n\n        map = [0] * 128\n        count = len(t)\n        start = 0\n        end = 0\n        min_len = float(\'inf\')\n        start_index = 0\n        # UPVOTE !\n        for char in t:\n            map[ord(char)] += 1\n\n        while end < len(s):\n            if map[ord(s[end])] > 0:\n                count -= 1\n            map[ord(s[end])] -= 1\n            end += 1\n\n            while count == 0:\n                if end - start < min_len:\n                    start_index = start\n                    min_len = end - start\n\n                if map[ord(s[start])] == 0:\n                    count += 1\n                map[ord(s[start])] += 1\n                start += 1\n\n        return "" if min_len == float(\'inf\') else s[start_index:start_index + min_len]\n```\n```C# []\npublic class Solution {\n    public string MinWindow(string s, string t) {\n        if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(t) || s.Length < t.Length) {\n            return "";\n        }\n\n        int[] map = new int[128];\n        int count = t.Length;\n        int start = 0, end = 0, minLen = int.MaxValue, startIndex = 0;\n        /// UPVOTE !\n        foreach (char c in t) {\n            map[c]++;\n        }\n\n        char[] chS = s.ToCharArray();\n\n        while (end < chS.Length) {\n            if (map[chS[end++]]-- > 0) {\n                count--;\n            }\n\n            while (count == 0) {\n                if (end - start < minLen) {\n                    startIndex = start;\n                    minLen = end - start;\n                }\n\n                if (map[chS[start++]]++ == 0) {\n                    count++;\n                }\n            }\n        }\n\n        return minLen == int.MaxValue ? "" : new string(chS, startIndex, minLen);\n    }\n}\n```\n```Go []\nfunc minWindow(s string, t string) string {\n\tif len(s) == 0 || len(t) == 0 || len(s) < len(t) {\n\t\treturn ""\n\t}\n\n\tmapS := make([]int, 128)\n\tcount := len(t)\n\tstart, end := 0, 0\n\tminLen, startIndex := int(^uint(0)>>1), 0\n    /// UPVOTE !\n\tfor _, char := range t {\n\t\tmapS[char]++\n\t}\n\n\tfor end < len(s) {\n\t\tif mapS[s[end]] > 0 {\n\t\t\tcount--\n\t\t}\n\t\tmapS[s[end]]--\n\t\tend++\n\n\t\tfor count == 0 {\n\t\t\tif end-start < minLen {\n\t\t\t\tstartIndex = start\n\t\t\t\tminLen = end - start\n\t\t\t}\n\n\t\t\tif mapS[s[start]] == 0 {\n\t\t\t\tcount++\n\t\t\t}\n\t\t\tmapS[s[start]]++\n\t\t\tstart++\n\t\t}\n\t}\n\n\tif minLen == int(^uint(0)>>1) {\n\t\treturn ""\n\t}\n\n\treturn s[startIndex : startIndex+minLen]\n}\n```\n```Ruby []\nclass Solution\n  def min_window(s, t)\n    return "" if s.empty? || t.empty? || s.length < t.length\n\n    map = Array.new(128, 0)\n    count = t.length\n    start = 0\n    end_ = 0\n    min_len = Float::INFINITY\n    start_index = 0\n    # UPVOTE !\n    t.each_char { |char| map[char.ord] += 1 }\n\n    while end_ < s.length\n      if map[s[end_].ord] > 0\n        count -= 1\n      end\n      map[s[end_].ord] -= 1\n      end_ += 1\n\n      while count == 0\n        if end_ - start < min_len\n          start_index = start\n          min_len = end_ - start\n        end\n\n        if map[s[start].ord] == 0\n          count += 1\n        end\n        map[s[start].ord] += 1\n        start += 1\n      end\n    end\n\n    min_len == Float::INFINITY ? "" : s[start_index, min_len]\n  end\nend\n```\n![image.png](https://assets.leetcode.com/users/images/851cc01f-ed3b-4fc4-a9d4-50d0e6b450c5_1707006379.5967262.png)\n
['C++', 'Java', 'Go', 'Python3', 'Ruby', 'C#']