Skip to content

Commit 8038b02

Browse files
committed
添加 problem 567
1 parent fed1698 commit 8038b02

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode
2+
3+
func checkInclusion(s1 string, s2 string) bool {
4+
var freq [256]int
5+
if len(s2) == 0 || len(s2) < len(s1) {
6+
return false
7+
}
8+
for i := 0; i < len(s1); i++ {
9+
freq[s1[i]-'a']++
10+
}
11+
left, right, count := 0, 0, len(s1)
12+
13+
for right < len(s2) {
14+
if freq[s2[right]-'a'] >= 1 {
15+
count--
16+
}
17+
freq[s2[right]-'a']--
18+
right++
19+
if count == 0 {
20+
return true
21+
}
22+
if right-left == len(s1) {
23+
if freq[s2[left]-'a'] >= 0 {
24+
count++
25+
}
26+
freq[s2[left]-'a']++
27+
left++
28+
}
29+
}
30+
return false
31+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question567 struct {
9+
para567
10+
ans567
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para567 struct {
16+
s string
17+
p string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans567 struct {
23+
one bool
24+
}
25+
26+
func Test_Problem567(t *testing.T) {
27+
28+
qs := []question567{
29+
30+
question567{
31+
para567{"ab", "abab"},
32+
ans567{true},
33+
},
34+
35+
question567{
36+
para567{"abc", "cbaebabacd"},
37+
ans567{true},
38+
},
39+
40+
question567{
41+
para567{"abc", ""},
42+
ans567{false},
43+
},
44+
45+
question567{
46+
para567{"abc", "abacbabc"},
47+
ans567{true},
48+
},
49+
50+
question567{
51+
para567{"ab", "eidboaoo"},
52+
ans567{false},
53+
},
54+
}
55+
56+
fmt.Printf("------------------------Leetcode Problem 567------------------------\n")
57+
58+
for _, q := range qs {
59+
_, p := q.ans567, q.para567
60+
fmt.Printf("【input】:%v 【output】:%v\n", p, checkInclusion(p.s, p.p))
61+
}
62+
fmt.Printf("\n\n\n")
63+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [567. Permutation in String](https://leetcode.com/problems/permutation-in-string/)
2+
3+
## 题目
4+
5+
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of the second string.
6+
7+
8+
Example 1:
9+
10+
```c
11+
Input:s1 = "ab" s2 = "eidbaooo"
12+
Output:True
13+
Explanation: s2 contains one permutation of s1 ("ba").
14+
```
15+
16+
Example 2:
17+
18+
```c
19+
Input:s1= "ab" s2 = "eidboaoo"
20+
Output: False
21+
```
22+
23+
Note:
24+
25+
1. The input strings only contain lower case letters.
26+
2. The length of both given strings is in range [1, 10,000].
27+
28+
## 题目大意
29+
30+
31+
在一个字符串重寻找子串出现的位置。子串可以是 Anagrams 形式存在的。Anagrams 是一个字符串任意字符的全排列组合。
32+
33+
## 解题思路
34+
35+
这一题和第 438 题,第 3 题,第 76 题,第 567 题类似,用的思想都是"滑动窗口"。
36+
37+
38+
这道题只需要判断是否存在,而不需要输出子串所在的下标起始位置。所以这道题是第 438 题的缩水版。具体解题思路见第 438 题。
39+
40+
41+
42+
43+
44+

0 commit comments

Comments
 (0)