Repository navigation
Expand file tree
/
Copy pathMinimumWindowSubstring.cs
More file actions
61 lines (57 loc) · 1.86 KB
/
Copy pathMinimumWindowSubstring.cs
File metadata and controls
61 lines (57 loc) · 1.86 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
var inst = new Leetcode.MinimumWindowSubstring();
Console.WriteLine(inst.minWindow("ADOBECODEBANCBA", "ABC"));
*/
namespace Algorithm.Leetcode
{
class MinimumWindowSubstring
{
public String minWindow(String S, String T)
{
int count = 0, front = 0, end = 0, min = int.MaxValue, min_front=0;
Dictionary<char, short> record = new Dictionary<char, short>();
for (int i = 0; i < T.Length; i++)
{
record.Add(T[i], 0);
}
for (end = 0; end < S.Length; end++)
{
if (record.ContainsKey(S[end]))
{
++record[S[end]];
if (record[S[end]] == 1)
{
++count;
}
while (count == T.Length)
{
if (end - front + 1 < min)
{
min = end - front + 1;
min_front = front;
}
if (record.ContainsKey(S[front]))
{
--record[S[front]];
if (record[S[front]] == 0)
{
--count;
}
++front;
}
while (!record.ContainsKey(S[front]))
{
++front;
}
}
}
}
return S.Substring(min_front, min);
}
}
}