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 <stdio.h>
2#include <string.h>
3#include <stdbool.h>
4#define CHAR_COUNT 128
5
6bool isValid(char* s, char* t, int start, int end) {
7 int required[CHAR_COUNT] = {0}, window[CHAR_COUNT] = {0};
8 for(int i = 0; i < strlen(t); i++) required[t[i]]++;
9 for(int i = start; i < end; i++) window[s[i]]++;
10
11 for(int i = 0; i < CHAR_COUNT; i++) {
12 if (window[i] < required[i]) return false;
13 }
14 return true;
15}
16
17char* minWindow(char* s, char* t) {
18 int slen = strlen(s), tlen = strlen(t);
19 if (slen < tlen) return "";
20
21 int minLen = slen + 1, start = 0, end = 0;
22 char* result = "";
23
24 for (start = 0; start < slen; start++) {
25 for (end = start + tlen; end <= slen; end++) {
26 if (isValid(s, t, start, end) && (end - start) < minLen) {
27 minLen = end - start;
28 result = strndup(s + start, minLen);
29 }
30 }
31 }
32 return result;
33}
The C solution generates all start indexes and, for each, creates an end substring incrementally until it matches string "t". The isValid function is called to ensure the substring meets criteria. For legitimate substrings, it updates the minimal length found and extracts it for return.