Find the longest substring in alphabetical order

前端 未结 17 2479
有刺的猬
有刺的猬 2020-11-30 09:12

I have this code that I found on another topic, but it sorts the substring by contiguous characters and not by alphabetical order. How do I correct it for alphabetical order

17条回答
  •  甜味超标
    2020-11-30 09:44

    s = 'cyqfjhcclkbxpbojgkar'
    r = ''
    c = ''
    for char in s:
        if (c == ''):
            c = char
        elif (c[-1] <= char):
            c += char
        elif (c[-1] > char):
            if (len(r) < len(c)):
                r = c
                c = char
            else:
                c = char
    if (len(c) > len(r)):
        r = c
    print(r)
    

提交回复
热议问题