-
Notifications
You must be signed in to change notification settings - Fork 0
/
20190621-567.permutation-in-string.go
75 lines (66 loc) · 1.59 KB
/
20190621-567.permutation-in-string.go
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
package main
/*
* @lc app=leetcode id=567 lang=golang
*
* [567] Permutation in String
*/
/* 解法2:滑动窗口:进入窗口计数递减,出窗口计数递增 + 判断是否全为零 */
func checkInclusion(s1 string, s2 string) bool {
if len(s2) < len(s1) {
return false
}
counter := [26]int{}
for i := 0; i < len(s1); i++ {
counter[s1[i]-'a']++
counter[s2[i]-'a']--
}
if allZero(counter) {
return true
}
for i := len(s1); i < len(s2); i++ {
counter[s2[i]-'a']-- // 进入窗口递减
counter[s2[i-len(s1)]-'a']++ // 出窗口递增
if allZero(counter) {
return true
}
}
return false
}
func allZero(nums [26]int) bool {
for _, val := range nums {
if val != 0 {
return false
}
}
return true
}
/* 解法1:我的笨办法:不停的判断字串是否可由s1中的字符组成 */
func checkInclusion_1(s1 string, s2 string) bool {
alphabet := countOccurance(s1)
for i, j := 0, len(s1); j <= len(s2); i, j = i+1, j+1 {
if judgeCombination(alphabet, s2[i:j]) {
return true
}
}
return false
}
// countOccurance: 返字符串各个字符的出现次数, 及字符种类数目
func countOccurance(s1 string) []int {
alphabet := make([]int, 26)
for i := 0; i < len(s1); i++ {
alphabet[s1[i]-'a']++
}
return alphabet
}
func judgeCombination(alphabet []int, str string) bool {
copied := make([]int, len(alphabet))
copy(copied, alphabet)
for i := 0; i < len(str); i++ {
if copied[str[i]-'a'] > 0 {
copied[str[i]-'a']--
} else {
return false
}
}
return true
}