Finding longest substring in alphabetical order

后端 未结 17 1448
刺人心
刺人心 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:44

    There are many things to improve in your code but making minimum changes so as to make it work. The problem is you should have if last_pos(i) != None: in your for loop (i instead of i+1) and you should compare diff (not diff - 1) against maxLen. Please read other answers to learn how to do it better.

    for i in range(len(s)):
        if last_pos(i) != None:
            diff = last_pos(i) - i + 1
        if diff > maxLen:
            maxLen = diff
            startPos = i
            endPos = startPos + diff - 1
    

提交回复
热议问题