-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathPermutationinString.cpp
152 lines (107 loc) · 2.82 KB
/
PermutationinString.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Source: https://leetcode.com/problems/permutation-in-string/
1st approach
Time: O(m * n), where m is total number lower case english letters(26) and n is the length of the given string s2
Space: O(1), in-place
*/
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int s1Len = s1.length();
int s2Len = s2.length();
if(s1Len > s2Len) {
return false;
}
vector<int> s1CharFreq(26, 0);
vector<int> s2CharFreq(26, 0);
for(int i = 0; i < s1Len; ++i) {
++s1CharFreq[s1[i] - 'a'];
++s2CharFreq[s2[i] - 'a'];
}
for(int i = s1Len; i < s2Len; ++i) {
if(s1CharFreq == s2CharFreq) {
return true;
}
++s2CharFreq[s2[i] - 'a'];
--s2CharFreq[s2[i - s1Len] - 'a'];
}
return s1CharFreq == s2CharFreq;
}
};
---------------------------------------------------------------------------------------------------
/*
2nd approach (More optimized as we are using only a single vector(charFreq))
Time: (m * n), where m is total number lower case english letters(26) and n is the length of the given string s2
Space: O(1), in-place
*/
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int s1Len = s1.length();
int s2Len = s2.length();
if(s1Len > s2Len) {
return false;
}
vector<int> charFreq(26, 0);
for(int i = 0; i < s1Len; ++i) {
++charFreq[s1[i] - 'a'];
--charFreq[s2[i] - 'a'];
}
for(int i = s1Len; i < s2Len; ++i) {
if(isPerm(charFreq)) {
return true;
}
--charFreq[s2[i] - 'a'];
++charFreq[s2[i - s1Len] - 'a'];
}
return isPerm(charFreq);
}
private:
bool isPerm(vector<int>& charFreq) {
for(int i = 0; i < 26; ++i) {
if(charFreq[i] != 0) {
return false;
}
}
return true;
}
};
---------------------------------------------------------------------------------------------------
/*
3rd approach (Most optimized)
Time: O(n), where n is the length of the given string s2
Space: O(1), in-place, though we are using an array(charFreq) of size 26, but still the size will remain same for
all cases as the array size of 26 is fixed and doesn't depend on the input
*/
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int s1Len = s1.length();
int s2Len = s2.length();
if(s1Len > s2Len) {
return false;
}
int charFreq[26] = {};
for(int i = 0; i < s1Len; ++i) {
++charFreq[s1[i] - 'a'];
}
int i = 0;
int j = 0;
int totalChars = 0;
while(j < s2Len) {
if(charFreq[s2[j] - 'a']-- > 0) {
++totalChars;
}
if(totalChars == s1Len) {
return true;
}
if(++j - i == s1Len){
if(charFreq[s2[i] - 'a']++ >= 0) {
--totalChars;
}
++i;
}
}
return false;
}
};