438. Find All Anagrams in a String
来源:互联网 发布:深入浅出mysql第三版 编辑:程序博客网 时间:2024/06/11 04:37
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]]
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
阅读全文
0 0
- 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
- 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
- 438. Find All Anagrams in a String
- 438. Find All Anagrams in a String
- LeetCode Find All Anagrams in a String
- leetcode: Find All Anagrams in a String
- leetcode438. Find All Anagrams in a String
- leetcode438. Find All Anagrams in a String
- 【13.4】c++ primer plus 课后编程答案
- 【转】 C++ 右值引用详…
- kafka java代码编写
- crond和crontab的tag
- php中的session性能
- 438. Find All Anagrams in a String
- Solr对数据库中的CLOB字段建立索引的问题
- kmalloc vmalloc kzalloc malloc 和 get_free_page()
- 为什么要学习Kotlin
- ashx文件使用
- C++ 虚函数表解析
- Django Admin
- 代码探索
- [绍棠] Scrapy+Flask+Mongodb+Swift开发全攻略