python simple solution

  • 0
    class Solution(object):
        def findSubstringInWraproundString(self, p):
            :type p: str
            :rtype: int
            # store #substrings ends at some "char": counter["a"]
            counter = collections.defaultdict(int)
            # start is the position where the legal substring begins; prev stores the #legal substrings ends at previous position
            start, prev = 0, 1
            for i in range(len(p)):
                if i != 0 and ord(p[i])==ord(p[i-1])+1 or p[i-1:i+1] == "za":
                    counter[p[i]] = max(counter[p[i]], i - start + prev)
                    start, prev = i, 1
                    counter[p[i]] = max(counter[p[i]], 1)
            return sum(counter.values())

Log in to reply

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