Find the longest substring in alphabetical order

前端 未结 17 2433
有刺的猬
有刺的猬 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:37

    Wow, some really impressing code snippets here... I want to add my solution, as I think it's quite clean:

    s = 'cyqfjhcclkbxpbojgkar'
    
    res = ''
    tmp = ''
    
    for i in range(len(s)):
        tmp += s[i]
        if len(tmp) > len(res):
            res = tmp
        if i > len(s)-2:
            break
        if s[i] > s[i+1]:
            tmp = ''
    
    print("Longest substring in alphabetical order is: {}".format(res))
    

提交回复
热议问题