-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76.cpp
More file actions
50 lines (42 loc) · 1.27 KB
/
76.cpp
File metadata and controls
50 lines (42 loc) · 1.27 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
class Solution {
public:
int charToInt(char c){
if(c - 'a' >= 0 && c - 'z' <= 0) return c-'a';
else return c-'A'+26;
}
string minWindow(string s, string t) { //O(m + n), two pointers
vector<int> needs(52);
for(int i = 0; i < t.length(); i++){
needs[charToInt(t[i])]++;
}
vector<int> cur(52);
int numRight = 52;
for(int i = 0; i < 52; i++){
if(needs[i] > 0) numRight--;
}
int l = 0;
int r = -1;
int minLength = s.length()+1;
int lAns = -1;
while(l < s.length()){
while(r+1 < s.length() && numRight < 52){
r++;
int c = charToInt(s[r]);
if(cur[c] == needs[c]-1) numRight++;
cur[c]++;
}
if(numRight == 52){
if(r-l+1 < minLength){
minLength = r-l+1;
lAns = l;
}
}
int c = charToInt(s[l]);
if(cur[c] == needs[c]) numRight--;
cur[c]--;
l++;
}
if(minLength <= s.length()) return s.substr(lAns, minLength);
else return "";
}
};