Skip to content

Commit afb727f

Browse files
authored
Create Find All Anagrams in a String.py
1 parent 5d88935 commit afb727f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#438.Find All Anagrams in a String
2+
class Solution:
3+
def findAnagrams(self, s: str, p: str) -> List[int]:
4+
ans = []
5+
count = Counter(p)
6+
required = len(p)
7+
8+
for r, c in enumerate(s):
9+
count[c] -= 1
10+
if count[c] >= 0:
11+
required -= 1
12+
if r >= len(p):
13+
count[s[r - len(p)]] += 1
14+
if count[s[r - len(p)]] > 0:
15+
required += 1
16+
if required == 0:
17+
ans.append(r - len(p) + 1)
18+
19+
return ans

0 commit comments

Comments
 (0)