Created
September 25, 2017 13:43
-
-
Save luojiyin1987/39c7d55de13a4af216b6f70601a1b020 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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