Created
February 7, 2022 02:17
-
-
Save amarjitdhillon/a0da06c0236655552d4baaa2bb849f0d to your computer and use it in GitHub Desktop.
Find All Anagrams in a String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def findAnagrams(self, s: str, p: str) -> List[int]: | |
''' | |
1. len(P) >= len(S). if not return [] as there is no anagram | |
2. Take sliding window of len(P) and slide over the S array. Update the frequecy list for each window | |
if both lists are same then add the index to result list.append | |
3. At then end return res list | |
''' | |
res, charP, charS = [], [0]*26, [0]*26 | |
if len(p) > len(s): # edge case | |
return res | |
for c in p: # freq count of second word | |
charP[ord(c)-ord('a')] += 1 | |
for i in range(0, len(s)-len(p)+1): # iterate over the first word with sliding window of len(p) | |
if i == 0: # begining of the word | |
for j in range(i, i+len(p)): | |
charS[ord(s[j])-ord('a')] += 1 | |
else: # increment the end of sliding window count in charS | |
charS[ord(s[i+len(p)-1])-ord('a')] += 1 | |
if charS == charP: | |
res.append(i) | |
# decrement the starting of sliding window count in charS | |
charS[ord(s[i])-ord('a')] -= 1 | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment