Python


  • 0
    B
    class Solution(object):
        def checkInclusion(self, s1, s2):
            """
            :type s1: str
            :type s2: str
            :rtype: bool
            """
            if len(s1) < 1 or len(s2) < 1 or len(s2) < len(s1): return False
            hashmap = {}
            cnt = left = 0 
            for v in s1:
                if v not in hashmap: 
                    hashmap[v] = 1
                    cnt += 1
                else: hashmap[v] += 1
            for i in range(len(s1)):
                if s2[i] in hashmap:
                    hashmap[s2[i]] -= 1
                    if hashmap[s2[i]] == 0:
                        cnt -= 1
            if cnt == 0: return True
            right = len(s1)-1
            while right < len(s2) and cnt > 0:
                if s2[left] in hashmap:
                    hashmap[s2[left]] += 1
                    if hashmap[s2[left]] == 1:
                        cnt += 1
                left +=1
                right +=1
                if right < len(s2) and s2[right] in hashmap:
                    hashmap[s2[right]] -= 1
                    if hashmap[s2[right]] == 0:
                        cnt -= 1
                if cnt == 0 : return True
            return False
    

Log in to reply
 

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