Find the longest substring in alphabetical order

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

    Another way:

    s = input("Please enter a sentence: ")
    count = 0
    maxcount = 0
    result = 0
    for char in range(len(s)-1):
        if(s[char]<=s[char+1]):
           count += 1        
        if(count > maxcount):
               maxcount = count
               result = char + 1
    
        else:
    
            count = 0
    startposition = result - maxcount
    print("Longest substring in alphabetical order is: ", s[startposition:result+1])
    

提交回复
热议问题