I am trying to write a program that returns the length of longest substring within a string. This is my code:
def lengthOfLongestSubstring():
dict = {}
s = 'dvdf'
max_substr_length = 0
max_substr = ''
if len(s) < 1:
return 0
else:
for letter in s:
print('String value: ', s)
if letter not in max_substr:
max_substr = max_substr + letter
max_substr_length = len(max_substr)
dict[max_substr] = dict.get(max_substr, max_substr_length)
print(letter, max_substr, max_substr_length, dict)
elif letter in max_substr:
dict[max_substr] = dict.get(max_substr, max_substr_length)
s = s[s.index(letter)+1:]
max_substr = ''
max_substr_length = 0
print(s, letter, max_substr, max_substr_length, dict)
print(dict)
print(max(dict.values(), default=0))
For the input string s = 'dvdf'
I am getting rid of the first instance of the letter that gets repeated in the input string s, in line 18 of my code s = s[s.index(letter)+1:]
.
So when the second 'd' is encountered, s should get updated to s = 'vdf'
Unfortunately, the for loop doesn't start iterating from the 0th index of this new s. Is there a way that doesn't involve iterating over integer indexes to get the for loop to begin iterating from the beginning when the string is updated?