-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMinimum Window Substring
More file actions
45 lines (43 loc) · 1.44 KB
/
Minimum Window Substring
File metadata and controls
45 lines (43 loc) · 1.44 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
class Solution {
public:
string minWindow(string s, string t) {
//create the hash table for string t.
unordered_map<char, int> T;
unordered_map<char, bool> found;
int minLen= s.size()+1;
int minLeft=0,minRight;
for(int i = 0;i<t.size();i++)
{
T[t[i]]++;
found[t[i]] = true; //indicate the occurance.
}
//cout << "debug " << endl;
int count = 0;
for (int left=0,right=0;right<s.size();right++)
{
//get the char at end of string.
if(found[s[right]])//see right charcter in the T.
{
T[s[right]]--; //reduce the number of right character
if(T[s[right]]>=0) count++;
//cout << "debug 2" <<endl;
while(count == t.size())
{
if(right-left+1<minLen)
{
minLeft = left;
minRight = right;
minLen = right-left+1;
}
if(found[s[left]]) //see the left character in the T
{
T[s[left]]++;
if(T[s[left]]>0) count--; //it must be large than 0.
}
left++;
}
}
}
return minLen>s.size()?"":s.substr(minLeft,minLen);
}
};