Find All Anagrams in a String (Leetcode 438)
Problem Link: https://leetcode.com/problems/find-all-anagrams-in-a-string/
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
ans=[]
n=len(s)
dictofp={}
for i in p:
if(i in dictofp):
dictofp[i]+=1
else:
dictofp[i]=1
i=0
j=0
k=len(p)
tempdict={}
while(j<n):
if(s[j] in tempdict):
tempdict[s[j]]+=1
else:
tempdict[s[j]]=1
if(j-i+1<k):
j+=1
elif(j-i+1==k):
if(dictofp==tempdict):
ans.append(i)
if(tempdict[s[i]]==1):
del tempdict[s[i]]
else:
tempdict[s[i]]-=1
i+=1
j+=1
return ans
PreviousNumber of Sub-arrays of Size K and Average Greater than or Equal to Threshold (Leetcode 1343)NextPermutation in String (Leetcode 567)
Last updated