Last active
October 8, 2017 17:40
-
-
Save vetional/d97a8ab2d3e204976c87afa362de385f to your computer and use it in GitHub Desktop.
String problem created by vetional - https://repl.it/MRdh/1
This file contains hidden or 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
# Given a string, find the longest substring without repeating characters. | |
from collections import defaultdict | |
def solution(s): | |
if not s: | |
return None | |
tmp = "" | |
longest = "" | |
dict = defaultdict(int) | |
for i,c in enumerate(s): | |
if c not in dict: | |
dict[c] = 1 | |
tmp += c | |
else: | |
if len(tmp) > len(longest): | |
longest = tmp | |
dict.clear() | |
tmp = c | |
dict[c] = 1 | |
if len(longest) > len(tmp): | |
return longest | |
else: | |
return tmp | |
print solution("abccdefghhajajaajkluuioopwq") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment