Created
June 26, 2019 13:54
-
-
Save priyankvex/863991c2e5b2d12f95739f1a86ef8b5c to your computer and use it in GitHub Desktop.
Longest substring with unique characters
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
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