Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save willwangcc/c313fc605c7ef1501e4f187f9a2438c2 to your computer and use it in GitHub Desktop.
Save willwangcc/c313fc605c7ef1501e4f187f9a2438c2 to your computer and use it in GitHub Desktop.
# Time: O(n)
# Space: O(1)
# 3. Longest substring without repeating characters
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
longest, start, visited = 0, 0, [False for _ in range(256)] #ascii
for i, char in enumerate(s):
if visited[ord(char)]: # abca -b what if abcac
while char != s[start]: # a = Flase
visited[ord(s[start])] = False
start += 1
start += 1
else:
visited[ord(char)] = True
longest = max(longest, i - start + 1)
return longest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment