Python beat 94%


  • 0
    Y

    Basically to keep track of the count of characters in substrings of s2 by offsetting that of s1. Beat 94% upon submission.

        def checkInclusion(self, s1, s2):
            """
            :type s1: str
            :type s2: str
            :rtype: bool
            """
            if not s1: return True
            if len(s2) < len(s1): return False
            
            m, n, a = len(s1), len(s2), ord('a')
            count = [0] * 26
            for c in s1:
                count[ord(c)-a] += 1
            for i in xrange(0, m):
                count[ord(s2[i])-a] -= 1
            for i in xrange(m, n):
                if not any(count):
                    return True
                count[ord(s2[i-m])-a] += 1
                count[ord(s2[i])-a] -= 1
            return not any(count)
    

Log in to reply
 

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