438. Find All Anagrams in a String
2017-06-09 14:50
483 查看
class Solution(object):
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
res = []
pCounter = collections.Counter(p)
sCounter = collections.Counter(s[:len(p)-1])
for i in range(len(p)-1,len(s)):
sCounter[s[i]] += 1
if sCounter == pCounter:
res.append(i-len(p)+1)
sCounter[s[i-len(p)+1]] -= 1
if sCounter[s[i-len(p)+1]] == 0:
del sCounter[s[i-len(p)+1]]
return res
https://discuss.leetcode.com/topic/64412/python-sliding-window-solution-using-counter
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
res = []
pCounter = collections.Counter(p)
sCounter = collections.Counter(s[:len(p)-1])
for i in range(len(p)-1,len(s)):
sCounter[s[i]] += 1
if sCounter == pCounter:
res.append(i-len(p)+1)
sCounter[s[i-len(p)+1]] -= 1
if sCounter[s[i-len(p)+1]] == 0:
del sCounter[s[i-len(p)+1]]
return res
https://discuss.leetcode.com/topic/64412/python-sliding-window-solution-using-counter
相关文章推荐
- LeetCode 438 Find All Anagrams in a String
- 438. Find All Anagrams in a String
- 438 .Find All Anagrams in a String
- 438. Find All Anagrams in a String
- 438. Find All Anagrams in a String
- 438 Find All Anagrams in a String 找出字符串中所有的变位词
- [leetcode: Python]438. Find All Anagrams in a String
- 438. Find All Anagrams in a String
- 438. Find All Anagrams in a String
- 438. Find All Anagrams in a String*
- LeetCode438 Find All Anagrams in a String
- 438. Find All Anagrams in a String
- 438. Find All Anagrams in a String
- 438. Find All Anagrams in a String
- Leetcode——438. Find All Anagrams in a String
- HashTable-438-Find All Anagrams in a String
- 438. Find All Anagrams in a String
- Leetcode 438. Find All Anagrams in a String 找变位子串 解题报告
- 438. Find All Anagrams in a String
- 438. Find All Anagrams in a String