Permutation in String (Leetcode 567)

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

class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        d1={}
        d2={}
        n=len(s2)
        for i in s1:
            if(i not in d1):
                d1[i]=1
            else:
                d1[i]+=1
                
        i=0
        j=0
        
        while(j<n):
            if(s2[j] not in d2):
                d2[s2[j]]=1
            else:
                d2[s2[j]]+=1
            
            if(j-i+1<len(s1)):
                j+=1
            
            elif(j-i+1==len(s1)):
                if(d1==d2):
                    return True
                
                if(d2[s2[i]]==1):
                    del d2[s2[i]]
                else:
                    d2[s2[i]]-=1
                
                i+=1
                j+=1
        return False      

Last updated