Skip to content

Commit e49660e

Browse files
author
lightmen
committed
add find-all-anagrams-in-a-string[1].py
1 parent 33223ba commit e49660e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def findAnagrams(self, s, p):
3+
"""
4+
:type s: str
5+
:type p: str
6+
:rtype: List[int]
7+
"""
8+
ls, lp = len(s), len(p)
9+
cp = collections.Counter(p)
10+
ret = []
11+
count = lp
12+
13+
for i in range(ls):
14+
if cp[s[i]] > 0:
15+
count -= 1
16+
cp[s[i]] -= 1
17+
if i >= lp:
18+
if cp[s[i - lp]] >= 0:
19+
count += 1
20+
cp[s[i - lp]] += 1
21+
22+
if count == 0:
23+
ret.append(i - lp + 1)
24+
25+
return ret

0 commit comments

Comments
 (0)