Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created September 25, 2017 13:43
Show Gist options
  • Save luojiyin1987/39c7d55de13a4af216b6f70601a1b020 to your computer and use it in GitHub Desktop.
Save luojiyin1987/39c7d55de13a4af216b6f70601a1b020 to your computer and use it in GitHub Desktop.
func findAnagrams(s string, p string) []int {
ls := len(s)
lp := len(p)
count := lp
var temp []int
if ls == 0 || lp == 0 {
return temp
}
pMap := make(map[rune]int)
sRune := []rune(s)
pRune := []rune(p)
for _, v :=range pRune {
pMap[v]++
}
for i :=0 ; i<ls ; i++ {
if pMap[sRune[i]] >=1 {
count --
}
pMap[sRune[i]] --
if i >=lp {
if pMap[sRune[i-lp]] >=0 {
count ++
}
pMap[sRune[i-lp]] ++
}
if count == 0 {
temp = append(temp, i-lp+1)
}
}
return temp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment