File tree Expand file tree Collapse file tree 2 files changed +97
-0
lines changed
lcof2/剑指 Offer II 014. 字符串中的变位词 Expand file tree Collapse file tree 2 files changed +97
-0
lines changed Original file line number Diff line number Diff line change @@ -142,6 +142,57 @@ func check(window []int) bool {
142
142
}
143
143
```
144
144
145
+ ### ** C++**
146
+
147
+ ``` cpp
148
+ class Solution {
149
+ public:
150
+ bool checkInclusion(string s1, string s2) {
151
+
152
+ int len1 = s1.size();
153
+ int len2 = s2.size();
154
+
155
+ if (len2 < len1) {
156
+ return false;
157
+ }
158
+
159
+ int count[30 ] = {0};
160
+
161
+ for (int i = 0; i < len1; ++i) {
162
+ ++count[s1[i] - 'a'];
163
+ --count[s2[i] - 'a'];
164
+ }
165
+
166
+ int l = 0;
167
+ int r = len1 - 1;
168
+
169
+ while (r < len2) {
170
+
171
+ bool flag = true;
172
+
173
+ for (int i : count) {
174
+ if (i != 0) {
175
+ flag = false;
176
+ }
177
+ }
178
+
179
+ if (flag) {
180
+ return true;
181
+ }
182
+
183
+ if (r + 1 >= len2) {
184
+ break;
185
+ }
186
+
187
+ ++count[s2[l++] - 'a'];
188
+ --count[s2[++r] - 'a'];
189
+ }
190
+
191
+ return false;
192
+ }
193
+ };
194
+ ```
195
+
145
196
### ** ...**
146
197
147
198
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool checkInclusion (string s1, string s2) {
4
+
5
+ int len1 = s1.size ();
6
+ int len2 = s2.size ();
7
+
8
+ if (len2 < len1) {
9
+ return false ;
10
+ }
11
+
12
+ int count[30 ] = {0 };
13
+
14
+ for (int i = 0 ; i < len1; ++i) {
15
+ ++count[s1[i] - ' a' ];
16
+ --count[s2[i] - ' a' ];
17
+ }
18
+
19
+ int l = 0 ;
20
+ int r = len1 - 1 ;
21
+
22
+ while (r < len2) {
23
+
24
+ bool flag = true ;
25
+
26
+ for (int i : count) {
27
+ if (i != 0 ) {
28
+ flag = false ;
29
+ }
30
+ }
31
+
32
+ if (flag) {
33
+ return true ;
34
+ }
35
+
36
+ if (r + 1 >= len2) {
37
+ break ;
38
+ }
39
+
40
+ ++count[s2[l++] - ' a' ];
41
+ --count[s2[++r] - ' a' ];
42
+ }
43
+
44
+ return false ;
45
+ }
46
+ };
You can’t perform that action at this time.
0 commit comments