Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created December 3, 2014 04:39
Show Gist options
  • Save rohith2506/7a83d49405ec168bfdc3 to your computer and use it in GitHub Desktop.
Save rohith2506/7a83d49405ec168bfdc3 to your computer and use it in GitHub Desktop.
class Solution:
# @return an integer
def lengthOfLongestSubstring(self, s):
prev_length = 0
max_len = 0
char_dict = {}
for i,c in enumerate(s):
if c in char_dict.keys() and (i - char_dict[c] <= prev_length):
prev_length = (i - char_dict[c])
else:
prev_length = prev_length + 1
if prev_length > max_len:
max_len = prev_length
char_dict[c] = i
return max_len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment