Skip to content

Commit e6a1284

Browse files
authored
feat: add solutions to lcof2 problem: No.014 (doocs#724)
1 parent 40bb21c commit e6a1284

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

lcof2/剑指 Offer II 014. 字符串中的变位词/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,57 @@ func check(window []int) bool {
142142
}
143143
```
144144

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+
145196
### **...**
146197

147198
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
};

0 commit comments

Comments
 (0)