I am working on Leetcode challenge #3 (https://leetcode.com/problems/longest-substring-without-repeating-characters/)
Here is my solution using sliding window and a dictionary. I specifically added start = seen[s[i]]+1 to skip ahead. I am still told I am far slower than most people (for example, given abcdefgdabc, I am skipping abc when I see the second d. I thought this would save ton of time, but apparently this algorithm has a poor run time.
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
seen = {}
start = 0
max_size = 0
# check whether left pointer has reached the end yet
while start < len(s):
size = 0
for i in range(start, len(s)):
if s[i] in seen:
start = seen[s[i]]+1
seen = {}
break
else:
seen[s[i]] = i
size += 1
max_size = max(size, max_size)
return max_size
UPDATE
i = 0
j = 0
seen = {}
max_length = 0
while j < len(s):
if s[j] not in seen:
seen[s[j]] = j
j += 1
max_length = max(max_length, j-i)
else:
i = j = seen[s[j]] + 1
seen = {}
return max_length