Python, Sliding Window,hashmap


  • 0
    H
        def checkInclusion(self, s1, s2):
            """
            :type s1: str
            :type s2: str
            :rtype: bool
            """
            n1,n2=len(s1),len(s2)
            if n1==0: return True
            if n1>n2: return False
            mp,mp2={},{}
            for c in s1:
                mp[c]=mp.get(c,0)+1
            for i in xrange(n1):
                mp2[s2[i]]=mp2.get(s2[i],0)+1
            if mp2==mp: return True
            for i in xrange(n1,n2):
                mp2[s2[i]]=mp2.get(s2[i],0)+1
                mp2[s2[i-n1]]-=1
                if mp2[s2[i-n1]]==0: mp2.pop(s2[i-n1])
                if mp2==mp: return True
            return False

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.