Back to course home
0% completed
Vote For New Content
My solution
Mohammed Dh Abbas
Jul 10, 2024
import math from collections import Counter class Solution: def findStringAnagrams(self, text, pattern): result = [] if text == "": return result # pattern frequency count pattern_counter = Counter(pattern) # initial window frequency count text_counter = {} for i in range(len(pattern)): text_counter[text[i]] = text_counter.get(text[i], 0) text_counter[text[i]] += 1 i = len(pattern) - 1 j = 0 while i < len(text): # if counters match we have an anagram if text_counter == pattern_counter: result.append(j) # move i then add the i frequency to the map i += 1 if i == len(text): break text_counter[text[i]] = text_counter.get(text[i], 0) text_counter[text[i]] += 1 # remove the j frequency from the map then move j text_counter[text[j]] -= 1 if text_counter[text[j]] == 0: del text_counter[text[j]] j += 1 return result
0
0
Comments
Comments
On this page