-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0076-minimum-window-substring.c
51 lines (49 loc) · 1.41 KB
/
0076-minimum-window-substring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every
character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".
Space: O(1)
Time: O(n+m) where n is the length of s and m the length of t
*/
char * minWindow(char * s, char * t){
int alpha[59] = {0};
int need = 0;
for (int i=0; t[i]!='\0'; i++) { // Initialise the alphabet
if (alpha[t[i]-'A']==0)
need++;
alpha[t[i]-'A']--;
}
int maxLen = 100001;
int pos=0;
int i=0, j=0;
while (s[j]!='\0') {
alpha[s[j]-'A']++;
if (alpha[s[j]-'A']==0) {
need--;
if (need==0) {
while (alpha[s[i]-'A']>0) {
alpha[s[i]-'A']--;
i++;
}
if ((j-i+1)<maxLen) {
pos = i;
maxLen = j-i+1;
}
alpha[s[i]-'A']--;
need++;
i++;
}
}
j++;
}
if (maxLen==100001) { // No substring found
char* ans = malloc(sizeof(char));
ans[0] = '\0';
return ans;
} else {
char* ans = malloc(sizeof(char)*(maxLen+1));
for (int i=0; i<maxLen; i++)
ans[i] = s[i+pos];
ans[maxLen] = '\0';
return ans;
}
}