Python判断字符串中连续最长的递增英文字母

在字符串中找出连续最长的递增英文字母串(26个字母,不区分大小写),如果没有,返回空

解法:滑动窗口

示例 1:
输入:s = "79Efui"
输出:Ef

示例 1:
输入:s = "79Edfui"
输出:空

 

def longestStr(s):
    n = len(s)
    l = 0
    r = 0
    start = 0
    maxLen = 0

    while l < n:
        if s[l].isalpha():
            r = l + 1
            #后一个比前一个大1
            while r < n and s[r].isalpha() and ord(s[r].lower())-1 == ord(s[r-1].lower()):
                r += 1
            if r-l > maxLen:
                start = l
                maxLen = r-l
                
            l = r+1
            r = r+1
        else:
            l += 1
    return s[start:start+maxLen]

s1 = 'y7ww3DuEfWre5678y'
s2 = '678j8ui'
s3 = '78'
print(longestStr(s1))
print(longestStr(s2))
print(longestStr(s3))
滑动窗口

 

posted @ 2022-01-09 14:13  yudai  阅读(252)  评论(0编辑  收藏  举报