Skip to content

Commit a715c00

Browse files
add code for Permutation in String (567.cpp)
1 parent f540079 commit a715c00

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

leetcode/cpp/string/567.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
bool checkInclusion(string sml, string lrg) {
4+
if (lrg.length() < sml.length())
5+
return false;
6+
7+
unordered_map<char, int> targetMap, haveMap;
8+
for(char c: sml) {
9+
targetMap[c]++;
10+
}
11+
12+
for(char c: lrg.substr(0, sml.length())) {
13+
haveMap[c]++;
14+
}
15+
16+
int st=0, en=sml.length()-1;
17+
while(en < lrg.length()) {
18+
if (targetMap == haveMap) {
19+
return true;
20+
}
21+
22+
haveMap[lrg[st]]--;
23+
if (haveMap[lrg[st]]==0) haveMap.erase(lrg[st]);
24+
st++;
25+
en++;
26+
if (en<lrg.length())
27+
haveMap[lrg[en]]++;
28+
}
29+
return false;
30+
31+
}
32+
};

0 commit comments

Comments
 (0)