Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Enrique Fernández
My solution using Counter and defaultdict

Enrique Fernández

Dec 11, 2024

import math from collections import Counter, defaultdict class Solution:   def findStringAnagrams(self, str1, pattern):     result_indices = []     # TODO: Write your code here         patternReps = Counter(pattern)     windowReps = defaultdict(int)     start = 0     for end in range(len(str1)):       windowReps[str1[end]] += 1       if end - start + 1 == len(pattern):         if patternReps == windowReps:           result_indices.append(start)         windowReps[str1[start]] -= 1         if windowReps[str1[start]] == 0:           del windowReps[str1[start]]         start += 1         return result_indices

0

0

Comments
Comments

On this page