Finding longest substring in alphabetical order

后端 未结 17 1471
刺人心
刺人心 2020-12-06 15:15

EDIT: I am aware that a question with similar task was already asked in SO but I\'m interested to find out the problem in this specific piece of code. I am

17条回答
  •  再見小時候
    2020-12-06 15:46

    I suppose this is problem set question for CS6.00.1x on EDX. Here is what I came up with.

    s = raw_input("Enter the string: ")
    longest_sub = ""
    last_longest = ""
    for i in range(len(s)):
        if len(last_longest) > 0:
            if last_longest[-1] <= s[i]:
                last_longest += s[i]
            else:
                last_longest = s[i]
        else:
            last_longest = s[i]
        if len(last_longest) > len(longest_sub):
            longest_sub = last_longest
    print(longest_sub)
    

提交回复
热议问题