Created
September 25, 2017 12:54
-
-
Save luojiyin1987/47b5ae96126946a8c4cfa09fbb99f25e 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
| //python | |
| class Solution(object): | |
| def findAnagrams(self, s, p): | |
| """ | |
| :type s: str | |
| :type p: str | |
| :rtype: List[int] | |
| """ | |
| ls, lp = len(s), len(p) | |
| count = lp | |
| cp = collections.Counter(p) | |
| ans = [] | |
| for i in range(ls): | |
| if cp[s[i]] >=1 : | |
| count -= 1 | |
| cp[s[i]] -= 1 | |
| if i >= lp: | |
| if cp[s[i - lp]] >= 0: | |
| count += 1 | |
| cp[s[i - lp]] += 1 | |
| if count == 0: | |
| ans.append(i - lp + 1) | |
| return ans | |
| //-------------------------------------------- | |
| //go | |
| func findAnagrams(s string, p string) []int { | |
| ls := len(s) | |
| lp := len(p) | |
| 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 -lp ; i++ { | |
| tMap := make(map[rune]int) | |
| for _, v := range sRune[i:i+lp] { | |
| tMap[v]++ | |
| } | |
| if len(tMap) == len(pMap) { | |
| count :=0 | |
| for k, _ := range pMap { | |
| if tMap[k] == pMap[k] { | |
| count ++ | |
| }else { | |
| break | |
| } | |
| } | |
| if count == len(pMap) { | |
| temp = append(temp , i) | |
| } | |
| } | |
| } | |
| return temp | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment