Find the longest substring in alphabetical order

前端 未结 17 2435
有刺的猬
有刺的猬 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 10:00

    s=input()
    temp=s[0]
    output=s[0]
    for i in range(len(s)-1):
        if s[i]<=s[i+1]:
            temp=temp+s[i+1]
            if len(temp)>len(output):
                output=temp           
        else:
            temp=s[i+1]
    
    print('Longest substring in alphabetic order is:' + output)
    

提交回复
热议问题