Skip to content

Commit d07553e

Browse files
committed
Time: 8 ms (82.05%), Space: 7.4 MB (57.90%) - LeetHub
1 parent 4a4d8ef commit d07553e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
4+
bool valid(unordered_map<char,int>& mp){
5+
for(auto x: mp)
6+
if(x.second != 0)
7+
return false;
8+
return true;
9+
}
10+
bool checkInclusion(string s1, string s2) {
11+
// s1 <= s2
12+
int k = s1.size();
13+
int n = s2.size();
14+
15+
if(n < k)
16+
return false;
17+
18+
unordered_map<char,int> mp;
19+
for(auto x: s1)
20+
mp[x]++;
21+
22+
for(int i = 0; i < k; i++){
23+
mp[s2[i]]--;
24+
}
25+
26+
if(valid(mp))
27+
return true;
28+
29+
for(int i = k; i < n; i++){
30+
mp[s2[i]]--;
31+
mp[s2[i - k]]++;
32+
33+
if(valid(mp))
34+
return true;
35+
}
36+
37+
return false;
38+
}
39+
};

0 commit comments

Comments
 (0)