accepted answer with little comment


  • 0
    S
    class Solution(object):
        def findSubstringInWraproundString(self, p):
            """
            :type p: str
            :rtype: int
            """
            # lowercase string
            s = "abcdefghijklmnopqrstuvwxyz"
            
            i,count = 0 ,0 # i for current p index , count for total combination
            #  max length combination with starting char
            #  a:10  mean  start with a and all under which length is 10 is counted
            countlen = { e:0 for e in s } 
            
            # lindex = it's index of s of prev char in p
            # cindex = it's index of s of current char in p
            # i = current index in p
            lindex,cindex,i,lenp = None,None,0 ,len(p)
            
            c_start_at = None # continual subsequence start at
    
            while i < lenp:
                c = p[i]                    # c = current char 
                cindex = s.index(c)    # cindex = c'index in s
    
                # start new subsequence if just start or  current char is not next of prev in s 
                if lindex is None or (lindex+1)%26 != cindex%26:
                    c_start_at = i
                    if countlen.get(p[i],0) == 0:
                        count += 1
                        countlen[p[i]]=1
    
                else: # count all subsequence start between c_start_at and i-1,and end with i , except length is less than countlen
                    for j in range(c_start_at,i+1):
                        if countlen.get(p[j],0) >= i-j+1:
                            break
                        count += 1
                        countlen[p[j]] = i-j+1 # current max length of p[j]
            
                lindex = cindex
                i+=1    
            return count
    

Log in to reply
 

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