【467】 Unique Substrings in Wraparound String

We define the string base to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so base will look like this:

"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".

Given a string s, return the number of unique non-empty substrings of s are present in base.

def findSubstringInWraproundString( p):
    res = {i: 1 for i in p}
    l = 1
    for i, j in zip(p, p[1:]):
        l = l + 1 if (ord(j) - ord(i)) % 26 == 1 else 1
        res[j] = max(res[j], l)
    return sum(res.values())

posted @ 2023-02-04 16:34  莫大师兄  阅读(24)  评论(0)    收藏  举报