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

0% completed

Vote For New Content
Mohammed Dh Abbas
Simpler solution.

Mohammed Dh Abbas

Jul 9, 2024

The window is fixed in size so the solution should not be that complicated

import math from collections import Counter class Solution: def findPermutation(self, text, pattern): # edge case if text == "": return False pattern_frq = Counter(pattern) text_freq = {} # the initial window frequency count for i in range(len(pattern)): text_freq[text[i]] = text_freq.get(text[i], 0) text_freq[text[i]] += 1 # move the window and check the frequencies j = 0 i = len(pattern) - 1 while i < len(text): if text_freq == pattern_frq: return True # move i and add the i frequency to the map i += 1 if i == len(text): break text_freq[text[i]] = text_freq.get(text[i], 0) text_freq[text[i]] += 1 # delete the old j frequency from the map and move j text_freq[text[j]] -= 1 if text_freq[text[j]] == 0: del text_freq[text[j]] j += 1 return False

0

0

Comments
Comments

On this page