-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.cpp
More file actions
66 lines (56 loc) · 1.73 KB
/
MinimumWindowSubstring.cpp
File metadata and controls
66 lines (56 loc) · 1.73 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
62
63
64
65
66
//
// MinimumWindowSubstring.cpp
// LeetCode
//
// Created by gongshang on 15/9/1.
// Copyright (c) 2015年 ninerec. All rights reserved.
//
class Solution {
private:
const int MAX_INTEGER = 2147483647;
map<char, int> t_map;
map<char, int> s_map;
public:
string minWindow(string s, string t) {
int min = MAX_INTEGER;
int start;
int winSize = t.length();
int curSize = 0;
for (int i = 0; i < t.length(); ++i){
if (t_map.find(t[i]) != t_map.end())
{
t_map[t[i]] = t_map[t[i]] + 1;
} else {
t_map[t[i]] = 1;
s_map[t[i]] = 0;
}
}
int temp_start = -1;
for (int i = 0; i < s.length(); ++i){
if (t_map.find(s[i]) != t_map.end()){ // in the map
s_map[s[i]] = s_map[s[i]] + 1; // add s_map
if (curSize < winSize && s_map[s[i]] <= t_map[s[i]])
{
curSize++;
}
if (temp_start == -1)
{
temp_start = i;
}
if (curSize >= winSize)
{
while(s_map[s[temp_start]] > t_map[s[temp_start]]){ //去掉重复
s_map[s[temp_start]]--;
while (t_map.find(s[++temp_start]) == t_map.end()){}
}
if (min > (i - temp_start + 1))
{
start = temp_start;
min = i - temp_start + 1;
}
}
}
}
return (curSize < winSize)?"":s.substr(start, min);
}
};