467. Unique Substrings in Wraparound String

class Solution(object):
    def findSubstringInWraproundString(self, p):
        """
        :type p: str
        :rtype: int
        """
      
        if len(p)<=1: return len(p)
        res=collections.defaultdict(int)
        res[p[0]]=1
        #dp[i] represent number of strings end with p[i]
        dp=[1]*(len(p)+1)
        for i in xrange(1,len(p)):
            if ord(p[i])-ord(p[i-1])==1 or (p[i]=='a' and p[i-1]=='z'): 
                dp[i]=dp[i-1]+1
            #select the maximum number of strings
            res[p[i]]=max(dp[i],res[p[i]])
            
        return sum(res.values())

你可能感兴趣的:(467. Unique Substrings in Wraparound String)