-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path567. Permutation in String.cpp
43 lines (42 loc) · 1.02 KB
/
567. Permutation in String.cpp
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
class Solution {
public:
bool checkInclusion(string s1, string s2) {
if(s2.size() < s1.size())
return false;
map<char, int> m ;
for( auto c: s1)
m[c]++;
int count = m.size();
for(int i =0; i <s1.size(); i++)
{
if( m.find(s2[i]) != m.end() )
{
m[s2[i]]--;
if( m[s2[i]] == 0 )
count--;
}
}
if( count ==0 )
return true;
int i = 0 , j = s1.size();
for(; j<s2.size(); j++)
{
if( m.find(s2[i]) != m.end() )
{
m[s2[i]]++;
if( m[s2[i]] == 1 )
count++;
}
i++;
if( m.find(s2[j]) != m.end() )
{
m[s2[j]]--;
if( m[s2[j]] == 0 )
count--;
}
if( count==0 )
return true;
}
return false;
}
};