-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp076.c
74 lines (74 loc) · 1.8 KB
/
p076.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
char* minWindow(char* s, char* t) {
int limit[128] = {0};
int cur[128] = {0};
unsigned pos = 0, substrlen = 0;
unsigned slen = strlen(s), tlen = strlen(t), tmplen;
if (!tlen) return "";
unsigned to = 0, i;
unsigned equallessthan0 = 0;
for (i = 0; i < tlen; i++)
{
limit[t[i]]++;
}
for (i = 0; i < 128; i++)
{
cur[i] = limit[i];
if (!cur[i]) equallessthan0++;
}
for (i = 0; i < slen; i++)
{
cur[s[i]]--;
if (!cur[s[i]])
{
equallessthan0++;
if (equallessthan0 == 128)
{
substrlen = i+1;
to = i;
break;
}
}
}
if (i == slen) return "";
for (i = 0; i < slen-1; i++)
{
cur[s[i]]++;
if (cur[s[i]] > 0)
{
equallessthan0--;
for (to++; to < slen; to++)
{
cur[s[to]]--;
if (!cur[s[to]])
{
equallessthan0++;
if (equallessthan0 == 128)
{
tmplen = to-i;
if (tmplen < substrlen)
{
pos = i+1;
substrlen = tmplen;
}
break;
}
}
}
if (equallessthan0 < 128)
{
break;
}
}
else
{
tmplen = to-i;
if (tmplen < substrlen)
{
pos = i+1;
substrlen = tmplen;
}
}
}
s[pos+substrlen] = '\0';
return s+pos;
}