Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Longest Substring Without Repeating Characters
class Solution(object):
def lengthOfLongestSubstring(self, string: str) -> int:
sub = {}
cur_sub_start = 0
cur_len = 0
longest = 0
for i, letter in enumerate(string):
if letter in sub and sub[letter] >= cur_sub_start:
cur_sub_start = sub[letter] + 1
cur_len = i - sub[letter] #current - duplicate
sub[letter] = i
else:
sub[letter] = i
cur_len += 1
if cur_len > longest:
longest = cur_len
return(longest)
@ichux
Copy link

ichux commented Sep 3, 2020

Well done on this. I read through https://nextwebb.hashnode.dev/how-to-find-the-longest-substring-without-duplicates-in-characters-python-solution-ckehod6k4049mp2s17kl5b1hh and it's interesting.

I want to draw your attention on your not obeying PEP-8 rule here lengthOfLongestSubstring. It will be better named length_of_longest_substring

@nextwebb
Copy link
Author

nextwebb commented Sep 3, 2020

Well done on this. I read through https://nextwebb.hashnode.dev/how-to-find-the-longest-substring-without-duplicates-in-characters-python-solution-ckehod6k4049mp2s17kl5b1hh and it's interesting.

I want to draw your attention on your not obeying PEP-8 rule here lengthOfLongestSubstring. It will be better named length_of_longest_substring

oh camelCase! i'll take note of that subsequently 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment