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).
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 public string MinWindow(string s, string t) {
6 Dictionary<char, int> required = new Dictionary<char, int>();
7 Dictionary<char, int> window = new Dictionary<char, int>();
8 foreach(char c in t) {
9 if (!required.ContainsKey(c)) required[c] = 0;
10 required[c]++;
11 }
12
13 int left = 0, minLen = int.MaxValue, minStart = 0, count = 0;
14 for(int right = 0; right < s.Length; right++) {
15 char c = s[right];
16 if (!window.ContainsKey(c)) window[c] = 0;
17 window[c]++;
18 if (window[c] <= required[c]) count++;
19
20 while(count == t.Length) {
21 if (right - left + 1 < minLen) {
22 minLen = right - left + 1;
23 minStart = left;
24 }
25 char cl = s[left++];
26 if (window[cl] > 0) {
27 window[cl]--;
28 if (window[cl] < required[cl]) count--;
29 }
30 }
31 }
32 return minLen > s.Length ? "" : s.Substring(minStart, minLen);
33 }
34}
The C# solution deploys similar logic using Dictionary for fast character count management. It expands and contracts the window based on the current character counts and the necessity for them to match those defined by the string "t".
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.
1#include <string>
2#include <unordered_map>
3using namespace std;
4
5bool isValid(const unordered_map<char, int>& required, const unordered_map<char, int>& window) {
6 for (auto& it : required) {
7 if (window.at(it.first) < it.second) return false;
8 }
9 return true;
10}
11
12string minWindow(string s, string t) {
13 unordered_map<char, int> required, window;
14 for (char c : t) required[c]++;
15 string result = "";
16 int min_len = s.size() + 1;
17
18 for (int start = 0; start < s.size(); ++start) {
19 window.clear();
20 for (int end = start; end < s.size(); ++end) {
21 window[s[end]]++;
22 if (end - start + 1 >= t.length() && isValid(required, window)) {
23 if (end - start + 1 < min_len) {
24 min_len = end - start + 1;
25 result = s.substr(start, min_len);
26 }
27 break;
28 }
29 }
30 }
31 return result;
32}
The C++ solution employs nested loops for substring generation. It ensures each generated substring's validity using "isValid", comparing its character counts with those required by "t". It maintains minimal windows as the solution develops, utilizing C++ string substrings and unordered_maps.