mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-23 18:10:20 +08:00
34 lines
538 B
Go
34 lines
538 B
Go
package leetcode
|
|
|
|
func findAnagrams(s string, p string) []int {
|
|
var freq [256]int
|
|
result := []int{}
|
|
if len(s) == 0 || len(s) < len(p) {
|
|
return result
|
|
}
|
|
for i := 0; i < len(p); i++ {
|
|
freq[p[i]-'a']++
|
|
}
|
|
left, right, count := 0, 0, len(p)
|
|
|
|
for right < len(s) {
|
|
if freq[s[right]-'a'] >= 1 {
|
|
count--
|
|
}
|
|
freq[s[right]-'a']--
|
|
right++
|
|
if count == 0 {
|
|
result = append(result, left)
|
|
}
|
|
if right-left == len(p) {
|
|
if freq[s[left]-'a'] >= 0 {
|
|
count++
|
|
}
|
|
freq[s[left]-'a']++
|
|
left++
|
|
}
|
|
|
|
}
|
|
return result
|
|
}
|