We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Python3 - one loop counting sequences and branch out for xx.xx patterns:
defsubstrCount(n,s):tot=0count_sequence=0prev=''fori,vinenumerate(s):# first increase counter for all seperate characterscount_sequence+=1ifiand(prev!=v):# if this is not the first char in the string # and it is not same as previous char, # we should check for sequence x.x, xx.xx, xxx.xxx etc# and we know it cant be longer on the right side than# the sequence we already found on the left side.j=1while((i-j)>=0)and((i+j)<len(s))andj<=count_sequence:# make sure the chars to the right and left are equal# to the char in the previous found squenceifs[i-j]==prev==s[i+j]:# if so increase total score and step one step further outtot+=1j+=1else:# no need to loop any further if this loop did # not find an x.x patternbreak#if the current char is different from previous, reset counter to 1count_sequence=1tot+=count_sequenceprev=vreturntot
Special String Again
You are viewing a single comment's thread. Return to all comments →
Python3 - one loop counting sequences and branch out for xx.xx patterns: