Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save priyankvex/863991c2e5b2d12f95739f1a86ef8b5c to your computer and use it in GitHub Desktop.
Save priyankvex/863991c2e5b2d12f95739f1a86ef8b5c to your computer and use it in GitHub Desktop.
Longest substring with unique characters
class Solution(object):
def solve(self, s):
start = 0
end = 1
max_start = 0
max_end = 0
map = {}
map[s[0]] = 0
for i in range(1, len(s)):
if map.get(s[i]) is not None and map.get(s[i]) >= start:
index = map.get(s[i])
temp_len = index - start + 1
max_len = max_end - max_start + 1
if temp_len > max_len:
max_start = start
max_end = index
start = index + 1
map[s[i]] = i
end = len(s) - 1
if end - start + 1 > max_end - max_start + 1:
max_start = start
max_end = end
return s[max_start:max_end+1]
if __name__ == "__main__":
s = 'abbccde'
ans = Solution().solve(s)
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment