-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum Window Substring
72 lines (57 loc) · 1.8 KB
/
Minimum Window Substring
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
//https://leetcode.com/problems/minimum-window-substring/
class Solution {
public:
string minWindow(string s, string t) {
map < char , int > mp1 , mp2 ;
int n = s.length() ;
int m = t.length() ;
if ( n < m )
return "" ;
for ( int i = 0 ; i < m ; i ++ )
mp1[ t [ i ] ] ++ ;
int start = 0 , end = -1 ;
for ( int i = 0 ; i < n ; i ++ )
{
mp2 [ s [ i ] ] ++ ;
int flag = 1 ;
for ( auto j : mp1 )
{
if ( mp2 [ j.first ] < mp1 [ j.first ] )
{
flag = 0 ;
break ;
}
}
if ( flag )
{
end = i ;
break ;
}
}
if ( end == - 1)
return "" ;
int len = end - start + 1 ;
string ans = s.substr ( start , len ) ;
while ( start < n and end - start + 1 >= m )
{
//cout << start << " " << end<< endl ;
if ( end - start + 1 < len )
{
len = end - start + 1 ;
ans = s.substr ( start , len ) ;
//cout << start << " " << end << " " << ans << endl ;
}
char c = s [ start ] ;
mp2 [ s [ start ] ] -- ;
start ++ ;
while ( mp2 [ c ] < mp1 [ c ] and end < n )
{
end ++ ;
mp2 [ s [ end ] ] ++ ;
}
if ( mp2 [ c ] < mp1 [ c ] )
break ;
}
return ans ;
}
};