题解 | #牛群名字覆盖#
牛群名字覆盖
https://www.nowcoder.com/practice/e6cef4702e7841f59cf47daf9bafb241
package main import "math" func minWindow(s string, t string) string { mymap := make(map[rune]int) copymap := make(map[rune]int) count := 0 for _, v := range t { mymap[v]++ copymap[v]++ count++ } l, r := -1, -1 visitmap := make(map[rune]int) minlength := math.MaxInt result := "" for i, v := range s { if mymap[v] != 0 { if l == -1 { l = i } mymap[v]-- count-- } if l != -1 { visitmap[v]++ } if count == 0 { r = i for j := l; j <= r; j++ { now := rune(s[j]) visitmap[now]-- l++ if visitmap[now] < copymap[now] { break } } l-- temp := s[l : r+1] if len(temp) < minlength { minlength = len(temp) result = temp } mymap[rune(s[l])]++ count++ l++ for l <= r && copymap[rune(s[l])] == 0 { visitmap[rune(s[l])]-- l++ } } } if l == -1 || r == -1 { return "" } return result }