This approach uses a sliding window (two-pointer technique) along with hash maps to keep track of characters. One hash map counts all characters in the string "t", while another counts the current characters inside the window from the string "s". We expand the right pointer to include characters until we have a valid window containing all characters from "t", and then contract the left pointer to minimize the window size.
Time Complexity: O(m + n) since each character in "s" and "t" is processed at most twice.
Space Complexity: O(1) since the size of the hashmap is limited to 128 characters (ASCII set).
1import java.util.HashMap;
2import java.util.Map;
3
4class Solution {
5 public String minWindow(String s, String t) {
6 Map<Character, Integer> required = new HashMap<>();
7 Map<Character, Integer> window = new HashMap<>();
8 for (char c : t.toCharArray()) required.put(c, required.getOrDefault(c, 0) + 1);
9
10 int left = 0, minLen = Integer.MAX_VALUE, minStart = 0, count = 0;
11 for (int right = 0; right < s.length(); right++) {
12 char c = s.charAt(right);
13 window.put(c, window.getOrDefault(c, 0) + 1);
14 if (window.get(c) <= required.get(c)) count++;
15
16 while (count == t.length()) {
17 if (right - left + 1 < minLen) {
18 minLen = right - left + 1;
19 minStart = left;
20 }
21 char cl = s.charAt(left++);
22 window.put(cl, window.get(cl) - 1);
23 if (window.get(cl) < required.get(cl)) count--;
24 }
25 }
26 return minLen > s.length() ? "" : s.substring(minStart, minStart + minLen);
27 }
28}
The Java solution uses two hash maps, one to store the character count of "t" and another for the current window in "s". A sliding window is expanded and contracted based on valid counts, updating the minimal found substring when valid conditions are met.
This alternative approach generates all possible substrings of "s" and checks each to see if it contains all the characters of "t" (including counts). Although not optimal, it serves as a conceptual fundamental check against the sliding window optimal approach by explicitly comparing substrings.
Time Complexity: O(m^2 * n) because of the double loop over the length of "s" and the complete scan for each "t" character.
Space Complexity: O(1) as the storage is minimal beyond fixed character occurrences.
1import java.util.HashMap;
2
3public class Solution {
4 public String minWindow(String s, String t) {
5 if (s.length() < t.length()) return "";
6 HashMap<Character, Integer> required = new HashMap<>();
7 for (char c : t.toCharArray()) required.put(c, required.getOrDefault(c, 0) + 1);
8 String result = "";
9 int min_len = s.length() + 1;
10
11 for (int start = 0; start < s.length(); start++) {
12 HashMap<Character, Integer> window = new HashMap<>();
13 for (int end = start; end < s.length(); end++) {
14 char c = s.charAt(end);
15 window.put(c, window.getOrDefault(c, 0) + 1);
16 if (end - start + 1 >= t.length() && isValid(required, window)) {
17 if (end - start + 1 < min_len) {
18 min_len = end - start + 1;
19 result = s.substring(start, end + 1);
20 }
21 break;
22 }
23 }
24 }
25 return result;
26 }
27
28 private boolean isValid(HashMap<Character, Integer> required, HashMap<Character, Integer> window) {
29 for (Character c : required.keySet()) {
30 if (window.getOrDefault(c, 0) < required.get(c)) return false;
31 }
32 return true;
33 }
34}
The Java version uses two hashmaps: one for required characters and one for current window counts. Nested loops track substring boundaries. It checks for validity using an auxiliary function "isValid" which returns whether the substring holds the necessary character count, selecting minimal lengths for valid results.