Find the longest substring in alphabetical order

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

    finding the longest substring in alphabetical order in Python

    in python shell 'a' < 'b' or 'a' <= 'a' is True

    result = ''
    temp = ''
    for char in s:
        if (not temp or temp[-1] <= char):
            temp += char
        elif (temp[-1] > char):
            if (len(result) < len(temp)):
                result = temp
            temp = char
        
    if (len(temp) > len(result)):
        result = temp
        
    print('Longest substring in alphabetical order is:', result)
    

提交回复
热议问题