Java | TC: O(S+T) | SC: O(T) | Space-optimized Sliding Window using Two Pointers
java\n/**\n * Space-optimized Sliding Window using Two Pointers\n *\n * Time Complexity: O(S + T)\n *\n * Space Complexity: O(T)\n *\n * S = length of String s.
```java\n/**\n * Space-optimized Sliding Window using Two Pointers\n *\n * Time Complexity: O(S + T)\n *\n * Space Complexity: O(T)\n *\n * S = length of String s. T = length of String t\n */\nclass Solution1 {\n    public String minWindow(String s, String t) {\n        if (s == null || t == null) {\n            throw new IllegalArgumentException("Input string is null");\n        }\n        if (s.length() < t.length() || t.length() == 0) {\n            return "";\n        }\n\n        HashMap<Character, Integer> map = new HashMap<>();\n        for (int i = 0; i < t.length(); i++) {\n            map.put(t.charAt(i), map.getOrDefault(t.charAt(i), 0) + 1);\n        }\n\n        int start = 0;\n        int end = 0;\n        int charTLeft = t.length();\n        int minStart = 0;\n        int minLen = Integer.MAX_VALUE;\n\n        while (end < s.length()) {\n            char eChar = s.charAt(end);\n            if (map.containsKey(eChar)) {\n                int count = map.get(eChar);\n                if (count > 0) {\n                    charTLeft--;\n                }\n                map.put(eChar, count - 1);\n            }\n            end++;\n\n            while (charTLeft == 0) {\n                if (minLen > end - start) {\n                    minLen = end - start;\n                    minStart = start;\n                }\n                char sChar = s.charAt(start);\n                if (map.containsKey(sChar)) {\n                    int count = map.get(sChar);\n                    if (count == 0) {\n                        charTLeft++;\n                    }\n                    map.put(sChar, count + 1);\n                }\n                start++;\n            }\n        }\n\n        return minLen == Integer.MAX_VALUE ? "" : s.substring(minStart, minStart + minLen);\n    }\n}\n```\n\n---\n\nSolutions to other Sliding Window questions on LeetCode:\n- [340. Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/discuss/1496838/Java-or-TC:-O(N)-or-SC:-O(K)-or-One-Pass-Sliding-Window-using-LinkedHashMap)\n- [159. Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/discuss/1496840/Java-or-TC:-O(N)-or-SC:-O(1)-or-One-Pass-Sliding-Window-using-LinkedHashMap)\n- [438. Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/1500039/Java-or-TC:-O(S+P)-or-SC:-O(1)-or-Sliding-window-solution)\n- [3. Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/1500874/Java-or-TC:-O(N)-or-SC:-O(1)-or-Sliding-Window-using-HashMap-and-Two-Pointers)\n- [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/discuss/1500877/Java-or-Both-O(N)-and-O(N-logN)-solutions-with-O(1)-space-or-Sliding-Window-and-Binary-Search-solutions)\n- [219. Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/discuss/1500887/Java-or-TC:-O(N)-or-SC:-O(min(N-K))-or-Sliding-Window-using-HashSet)\n- [220. Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/discuss/1500895/Java-or-TC:-O(N)-or-SC:-O(min(NK))-or-Sliding-Window-using-Buckets)\n- [567. Permutation in String](https://leetcode.com/problems/permutation-in-string/discuss/1500902/Java-or-TC:-O(S2)-or-SC:-O(1)-or-Constant-space-Sliding-Window-solution)\n- [239. Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/discuss/1506048/Java-or-TC:-O(N)-or-SC:-O(K)-or-Using-Deque-as-Sliding-Window)\n- [480. Sliding Window Median](https://leetcode.com/problems/sliding-window-median/discuss/1507981/Java-or-TC:-O(N*logK)-or-SC:-(K)-or-Optimized-sliding-window-using-TreeSet)\n- [487. Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/discuss/1508045/Java-or-TC:-O(N)-or-SC:-O(1)-or-Four-solutions-with-Follow-up-handled)\n- [1004. Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/discuss/1508044/Java-or-TC:-O(N)-or-SC:-O(1)-or-One-Pass-Optimized-Sliding-Window)\n
['Two Pointers', 'String', 'Sliding Window', 'Java']