Created
January 28, 2018 03:16
-
-
Save felipecsl/8925b2bfa0833efc879010b5bbfdbb1f 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
def length_of_longest_substring(str) | |
if str.empty? | |
return 0 | |
end | |
map = {} | |
curr_start = 0 | |
max_length = 0 | |
found_start = 0 | |
found_end = 0 | |
i = 0 | |
begin | |
s = str[i] | |
if map[s].nil? | |
map[s] = 1 | |
i += 1 | |
else | |
if map.keys.size > max_length | |
# new max substring | |
found_start = curr_start | |
found_end = i | |
max_length = map.keys.size | |
end | |
map.clear | |
i = curr_start + 1 | |
curr_start += 1 | |
end | |
end while i < str.length | |
if map.keys.size > max_length | |
# new max substring | |
found_start = curr_start | |
found_end = str.length | |
max_length = map.keys.size | |
end | |
if max_length > 0 | |
str[found_start..found_end - 1].length | |
else | |
1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment