Sliding window: Permutation (but actually anagram) in string Python

PHOTO EMBED

Thu Sep 22 2022 20:42:29 GMT+0000 (Coordinated Universal Time)

Saved by @bryantirawan #python #neetcode #hashmap

class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        s1Dict = {}
    
        for char in s1: 
            if char not in s1Dict: 
                s1Dict[char] = 1 
            else: 
                s1Dict[char] += 1 

        left = 0 


        compareDict = {}

        for right in range((left + (len(s1) - 1)), len(s2)): 
            if s2[left] in s1: 
                compare = s2[left:right + 1] #makes a list of chars from left to right pointers 
                for char in compare: 
                    if char not in compareDict: 
                        compareDict[char] = 1 
                    else: 
                        compareDict[char] += 1 
                if compareDict == s1Dict: 
                    return True 
                else: 
                    compareDict.clear() 
                    left += 1 
                    continue 



            else: 
                left += 1 

        return False 
content_copyCOPY

Concept is comparing dictionaries of sliding window of s2's window with s1 dictionary

https://leetcode.com/problems/permutation-in-string/