Skip to content

Instantly share code, notes, and snippets.

@jones
Created March 2, 2015 03:06
Show Gist options
  • Save jones/06ac4acaa14ff5469d43 to your computer and use it in GitHub Desktop.
Save jones/06ac4acaa14ff5469d43 to your computer and use it in GitHub Desktop.
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
class Solution:
# @return an integer
def lengthOfLongestSubstring(self, s):
if s is None or len(s) == 0:
return 0
i, maxLen = 0, 0
seenVals = set() # seen characters
for j in range(len(s)):
if (s[j] in seenVals):
maxLen = max(maxLen, j-i)
while(s[i] != s[j]):
seenVals.remove(s[i])
i += 1
i += 1
else:
seenVals.add(s[j])
return max(maxLen, len(s)-i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment