I am trying to solve this problem on LeetCode (https://leetcode.com/problems/longest-substring-without-repeating-characters/)
I got a code but i don't understand what's the problem with it.
def lengthOfLongestSubstring(s):
letters = []
counter = 0
max_length = 0
if s == '':
return 0
if len(set(s)) == 1:
return 1
for i in range(len(s)):
print(s[i], max_length)
if s[i] not in letters:
letters.append(s[i])
if i == 0:
counter += 1
continue
elif s[i] != s[i - 1]:
counter += 1
else:
if counter > max_length:
max_length = counter
letters = []
letters.append(s[i - 1])
letters.append(s[i])
counter = 2
return max(max_length, counter)
print(lengthOfLongestSubstring("pwwkew"))
I stuck on inputs "pwwkew" and "dvdf"... Program work different on these inputs. Probably i misunderstand the algorithm, so please correct me. Thanks!