Skip to content

Instantly share code, notes, and snippets.

@vetional
Last active October 8, 2017 17:40
Show Gist options
  • Save vetional/d97a8ab2d3e204976c87afa362de385f to your computer and use it in GitHub Desktop.
Save vetional/d97a8ab2d3e204976c87afa362de385f to your computer and use it in GitHub Desktop.
String problem created by vetional - https://repl.it/MRdh/1
# 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