438.找到字符串中所有字母异位词
438.找到字符串中所有字母异位词
题解
大水题
代码
package main func findAnagrams(s string, p string) []int { var result []int wind := make(map[byte]int) need := make(map[byte]int) for i := range p { need[p[i]]++ } left, right, match := 0, 0, 0 for right < len(s) { c := s[right] right++ if need[c] != 0 { wind[c]++ if wind[c] == need[c] { match++ } } for right-left == len(p) { if match == len(need) { result = append(result, left) } c = s[left] left++ if need[c] != 0 { if wind[c] == need[c] { match-- } wind[c]-- } } } return result }