Skip to content

Instantly share code, notes, and snippets.

@senthil1216
Last active February 9, 2018 16:32
Show Gist options
  • Save senthil1216/98de2b3431159f9073c44f73bdadd886 to your computer and use it in GitHub Desktop.
Save senthil1216/98de2b3431159f9073c44f73bdadd886 to your computer and use it in GitHub Desktop.
LongestSubstring without repeating characters
class Solution(object):
def lengthOfLongestSubstring(self, s):
if not s or len(s) == 0: return 0
store = {}
max_len, prevIdx, idx, currChar = 0, 0, 0, ''
while idx < len(s):
currChar = s[idx]
if currChar not in store:
store[currChar] = idx
else:
curr_len = idx-prevIdx
max_len = max(max_len, curr_len)
currIdx = store[currChar]
prevIdx = max(prevIdx, currIdx+1)
store[currChar] = idx
idx += 1
max_len = max(max_len, idx-prevIdx)
return max_len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment