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).
1#include <string>
2#include <unordered_map>
3using namespace std;
4
5class Solution {
6public:
7 string minWindow(string s, string t) {
8 unordered_map<char, int> required, window;
9 for(char c : t) ++required[c];
10
11 int left = 0, count = 0, min_len = s.size() + 1, min_start = 0;
12 for(int right = 0; right < s.size(); ++right) {
13 char c = s[right];
14 window[c]++;
15 if (window[c] <= required[c]) ++count;
16
17 while(count == t.size()) {
18 if (right - left + 1 < min_len) {
19 min_len = right - left + 1;
20 min_start = left;
21 }
22 char lc = s[left++];
23 if (window[lc] > 0) {
24 window[lc]--;
25 if (window[lc] < required[lc]) --count;
26 }
27 }
28 }
29 return min_len > s.size() ? "" : s.substr(min_start, min_len);
30 }
31};
This C++ solution leverages the STL's unordered_map to count characters in "t" and track characters in the current window in "s". The algorithm uses two pointers to create a sliding window. It attempts to contract the window as much as possible while maintaining a valid substring by shrinking from the left.
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.
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 public string MinWindow(string s, string t) {
6 if (s.Length < t.Length) return "";
7 Dictionary<char, int> required = new Dictionary<char, int>();
8 foreach (char c in t) {
9 if (!required.ContainsKey(c)) required[c] = 0;
10 required[c]++;
11 }
12
13 string result = "";
14 int min_len = int.MaxValue;
15
16 for (int start = 0; start < s.Length; ++start) {
17 Dictionary<char, int> window = new Dictionary<char, int>();
18 for (int end = start; end < s.Length; ++end) {
19 char c = s[end];
20 if (!window.ContainsKey(c)) window[c] = 0;
21 window[c]++;
22
23 if (end - start + 1 >= t.Length && IsValid(required, window)) {
24 if (end - start + 1 < min_len) {
25 min_len = end - start + 1;
26 result = s.Substring(start, end - start + 1);
27 }
28 break;
29 }
30 }
31 }
32 return result;
33 }
34
35 private bool IsValid(Dictionary<char, int> required, Dictionary<char, int> window) {
36 foreach (var kvp in required) {
37 if (window.GetValueOrDefault(kvp.Key) < kvp.Value) return false;
38 }
39 return true;
40 }
41}
The C# code illustrates a comprehensible strategy utilizing Dictionaries that represent character needs and real-time window dynamics. It checks each substring for completeness via "IsValid", maintaining shortest valid subsequences alongside extensive checks across "s".