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
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