567.字符串的排列
567.字符串的排列
题解
问s2 是否包含 s1 的排列,那只有当窗口长度等于s1长度,并且字符全有的时候才包含
代码
package main func checkInclusion(s1 string, s2 string) bool { wind := make(map[byte]int) need := make(map[byte]int) for i := range s1 { need[s1[i]]++ } left, right, match := 0, 0, 0 for right < len(s2) { c := s2[right] right++ if need[c] != 0 { wind[c]++ if wind[c] == need[c] { match++ } } for right-left == len(s1) { if match == len(need) { return true } c = s2[left] left++ if need[c] != 0 { if wind[c] == need[c] { match-- } wind[c]-- } } } return false }