Short Python Sliding Window O(n) time O(1) space


  • 0

    I tried math method to solve this problem but I found it is too tricky to deal with duplicates. Then, I use a dictionary to store max length that ends at a certain character.

    class Solution(object):
        def findSubstringInWraproundString(self, p):
            """
            :type p: str
            :rtype: int
            """
            d = {}
            cnt = 0
            for i in range(len(p)):
                if i > 0 and (ord(p[i]) - ord(p[i-1]) == 1 or ord(p[i-1]) - ord(p[i]) == 25):
                    cnt += 1
                else:
                    cnt = 1
                d[ord(p[i])] = max(d.get(ord(p[i]), 0), cnt)
            return sum(d.values())
    

Log in to reply
 

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