-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutation in String
43 lines (39 loc) · 1 KB
/
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
class Solution {
public:
bool ispermutation(string str1, string str2)
{
// Create 2 count arrays and initialize
// all values as 0
int count1[256] = {0};
int count2[256] = {0};
int i;
// For each character in input strings,
// increment count in the corresponding
// count array
for (i = 0; str1[i] && str2[i]; i++)
{
count1[str1[i]]++;
count2[str2[i]]++;
}
// If both strings are of different length.
// Removing this condition will make the
// program fail for strings like "aaca"
// and "aca"
if (str1[i] || str2[i])
return false;
// Compare count arrays
for (i = 0; i < 256; i++)
if (count1[i] != count2[i])
return false;
return true;
}
bool checkInclusion(string s1, string s2) {
int n=s1.size(),m=s2.size();
for(int i=0;i<m-n+1;i++){
if(ispermutation(s2.substr(i,n),s1)){
return true;
}
}
return false;
}
};