Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca66451

Browse files
authored
feat: add solutions to lc problem: No.0567 (#840)
No.0567.Permutation in String
1 parent 67a6fac commit ca66451

File tree

4 files changed

+169
-21
lines changed

4 files changed

+169
-21
lines changed

‎solution/0500-0599/0567.Permutation in String/README.md‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,33 @@
4747
<!-- 这里可写当前语言的特殊实现逻辑 -->
4848

4949
```python
50-
50+
class Solution:
51+
def checkInclusion(self, s1: str, s2: str) -> bool:
52+
need, window = {}, {}
53+
validate, left, right = 0, 0, 0
54+
for c in s1:
55+
window[c] = 0
56+
if c in need:
57+
need[c] += 1
58+
else:
59+
need[c] = 1
60+
61+
for right in range(len(s2)):
62+
c = s2[right]
63+
if c in need:
64+
window[c] += 1
65+
if window[c] == need[c]:
66+
validate += 1
67+
while right - left + 1 >= len(s1):
68+
if validate == len(need):
69+
return True
70+
d = s2[left]
71+
left += 1
72+
if d in need:
73+
if window[d] == need[d]:
74+
validate -= 1
75+
window[d] -= 1
76+
return False
5177
```
5278

5379
### **Java**
@@ -158,6 +184,37 @@ impl Solution {
158184
}
159185
```
160186

187+
### **Go**
188+
189+
```go
190+
func checkInclusion(s1 string, s2 string) bool {
191+
need, window := make(map[byte]int), make(map[byte]int)
192+
validate, left, right := 0, 0, 0
193+
for i := range s1 {
194+
need[s1[i]] += 1
195+
}
196+
for ; right < len(s2); right++ {
197+
c := s2[right]
198+
window[c] += 1
199+
if need[c] == window[c] {
200+
validate++
201+
}
202+
for right-left+1 >= len(s1) {
203+
if validate == len(need) {
204+
return true
205+
}
206+
d := s2[left]
207+
if need[d] == window[d] {
208+
validate--
209+
}
210+
window[d] -= 1
211+
left++
212+
}
213+
}
214+
return false
215+
}
216+
```
217+
161218
### **...**
162219

163220
```

‎solution/0500-0599/0567.Permutation in String/README_EN.md‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,33 @@
3939
### **Python3**
4040

4141
```python
42-
42+
class Solution:
43+
def checkInclusion(self, s1: str, s2: str) -> bool:
44+
need, window = {}, {}
45+
validate, left, right = 0, 0, 0
46+
for c in s1:
47+
window[c] = 0
48+
if c in need:
49+
need[c] += 1
50+
else:
51+
need[c] = 1
52+
# sliding window
53+
for right in range(len(s2)):
54+
c = s2[right]
55+
if c in need:
56+
window[c] += 1
57+
if window[c] == need[c]:
58+
validate += 1
59+
while right - left + 1 >= len(s1):
60+
if validate == len(need):
61+
return True
62+
d = s2[left]
63+
left += 1
64+
if d in need:
65+
if window[d] == need[d]:
66+
validate -= 1
67+
window[d] -= 1
68+
return False
4369
```
4470

4571
### **Java**
@@ -143,6 +169,38 @@ impl Solution {
143169
}
144170
```
145171

172+
### **Go**
173+
174+
```go
175+
func checkInclusion(s1 string, s2 string) bool {
176+
need, window := make(map[byte]int), make(map[byte]int)
177+
validate, left, right := 0, 0, 0
178+
for i := range s1 {
179+
need[s1[i]] += 1
180+
}
181+
for ; right < len(s2); right++ {
182+
c := s2[right]
183+
window[c] += 1
184+
if need[c] == window[c] {
185+
validate++
186+
}
187+
// shrink window
188+
for right-left+1 >= len(s1) {
189+
if validate == len(need) {
190+
return true
191+
}
192+
d := s2[left]
193+
if need[d] == window[d] {
194+
validate--
195+
}
196+
window[d] -= 1
197+
left++
198+
}
199+
}
200+
return false
201+
}
202+
```
203+
146204
### **...**
147205

148206
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func checkInclusion(s1 string, s2 string) bool {
2+
need, window := make(map[byte]int), make(map[byte]int)
3+
validate, left, right := 0, 0, 0
4+
for i := range s1 {
5+
need[s1[i]] += 1
6+
}
7+
for ; right < len(s2); right++ {
8+
c := s2[right]
9+
window[c] += 1
10+
if need[c] == window[c] {
11+
validate++
12+
}
13+
// shrink window
14+
for right-left+1 >= len(s1) {
15+
if validate == len(need) {
16+
return true
17+
}
18+
d := s2[left]
19+
if need[d] == window[d] {
20+
validate--
21+
}
22+
window[d] -= 1
23+
left++
24+
}
25+
}
26+
return false
27+
}
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
class Solution:
2-
def checkInclusion(self, s1, s2):
3-
"""
4-
:type s1: str
5-
:type s2: str
6-
:rtype: bool
7-
"""
8-
if len(s1) > len(s2):
9-
return False
10-
flag1 = [0] * 26
11-
flag2 = [0] * 26
12-
for i, x in enumerate(s1):
13-
flag1[ord(x) - ord('a')] += 1
14-
15-
for i, x in enumerate(s2):
16-
flag2[ord(x) - ord('a')] += 1
17-
if i >= len(s1):
18-
flag2[ord(s2[i - len(s1)]) - ord('a')] -= 1
19-
if flag1 == flag2:
20-
return True
2+
def checkInclusion(self, s1: str, s2: str) -> bool:
3+
need, window = {}, {}
4+
validate, left, right = 0, 0, 0
5+
for c in s1:
6+
window[c] = 0
7+
if c in need:
8+
need[c] += 1
9+
else:
10+
need[c] = 1
11+
# sliding window
12+
for right in range(len(s2)):
13+
c = s2[right]
14+
if c in need:
15+
window[c] += 1
16+
if window[c] == need[c]:
17+
validate += 1
18+
while right - left + 1 >= len(s1):
19+
if validate == len(need):
20+
return True
21+
d = s2[left]
22+
left += 1
23+
if d in need:
24+
if window[d] == need[d]:
25+
validate -= 1
26+
window[d] -= 1
2127
return False

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /