forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path567.cpp
More file actions
32 lines (27 loc) · 793 Bytes
/
567.cpp
File metadata and controls
32 lines (27 loc) · 793 Bytes
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
class Solution {
public:
bool checkInclusion(string sml, string lrg) {
if (lrg.length() < sml.length())
return false;
unordered_map<char, int> targetMap, haveMap;
for(char c: sml) {
targetMap[c]++;
}
for(char c: lrg.substr(0, sml.length())) {
haveMap[c]++;
}
int st=0, en=sml.length()-1;
while(en < lrg.length()) {
if (targetMap == haveMap) {
return true;
}
haveMap[lrg[st]]--;
if (haveMap[lrg[st]]==0) haveMap.erase(lrg[st]);
st++;
en++;
if (en<lrg.length())
haveMap[lrg[en]]++;
}
return false;
}
};