Created
July 15, 2017 00:31
-
-
Save willwangcc/c313fc605c7ef1501e4f187f9a2438c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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