Участник:Krivosheev.ah/unique-substrings-in-wraparound-string

Материал из DISCOPAL
Перейти к: навигация, поиск

Python3

https://leetcode.com/problems/unique-substrings-in-wraparound-string/

class Solution:
    def findSubstringInWraproundString(self, p: str) -> int:
        d,ret,cot,ind=defaultdict(int),len(set(p)),1,1
        for i in set(p):
            d[i]=1
        while ind <len(p):        
            while ind<len(p) and (ord(p[ind])==ord(p[ind-1])+1 or ( p[ind]=='a' and p[ind-1]=='z')):
                cot+=1               
                if  cot >d[p[ind]]:
                    ret+=(cot-d[p[ind]])
                    d[p[ind]]=cot               
                ind+=1              
            cot=1
            ind+=1
        return ret