-
Notifications
You must be signed in to change notification settings - Fork 1
/
minWindow.cpp
66 lines (60 loc) · 2.08 KB
/
minWindow.cpp
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
class Solution {
public:
string minWindow(string s, string t) {
// T holds all characters in string t and found has the one which was found
map <char, int > T;
map <char, int> found;
map <char,int>::iterator it;
int start = -1, end;
int len=0;
for (int i = 0; i < t.length(); i++ )
T[t[i]]++;
int nextpos;
char prev = ' ';
int minWindow;
// traverse the array s and try to find if present in T.
it = T.begin();
while ( s.length() - nextpos >= 0){
for (int i = nextpos; i< s.length(); i++ )
{
if (len!=0)
len++;
// tries to find if it is present in the map and updates start
if (T.size() > 0) {
it = T.find (s[i]);
if ( it!= T.end() ) {
if (prev != ' ' ){
nextpos = i;
prev = ' ';
}
if (T[s[i]] == 1 )
T.erase (s[i]);
else
T[s[i]]--;
cout << "size of T :" << T.size();
if (start == -1){
start = i;
len = 1;
prev = s[i];
}
}
}
if (T.size() == 0) {
end = i;
string str = s.substr(start, len);
cout <<str;
break;
}
}
start = -1;
len = 0;
for (int i = 0; i < t.length(); i++ )
T[t[i]]++;
}
cout << "start: "<< start << "end : " << end << "len : " << len;
// substr( pos, len)
string str = s.substr(start, len);
cout << str;
return str;
}
};