-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path567. Permutation in String
65 lines (52 loc) · 1.37 KB
/
567. Permutation in String
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
public:
bool isEqual(vector<int>&count1, vector<int>&count2)
{
for(int i=0; i<26; i++)
{
if(count1[i] != count2[i])
return false;
}
return true;
}
bool checkInclusion(string s1, string s2)
{
//count the character in s1
vector<int> count1(26);
for(int i=0; i<s1.length(); i++)
{
int index = s1[i]-'a';
count1[index]++;
}
//traverse s2 string in window size of s1 length and compare
int i=0;
int range = s1.length();
//count the characters in s2
vector<int> count2(26);
// first range in s2
while(i<range && i<s2.length())
{
int index = s2[i]-'a';
count2[index]++;
i++;
}
if(isEqual(count1, count2))
return true;
// next other ranges
while(i<s2.length())
{
//add the new character
char newChar = s2[i];
int index = newChar - 'a';
count2[index]++;
//remove the old character
char oldChar = s2[i-range];
index = oldChar - 'a';
count2[index]--;
if(isEqual(count1, count2))
return true;
i++;
}
return false;
}
};