Skip to content

Instantly share code, notes, and snippets.

@felipecsl
Created January 28, 2018 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipecsl/8925b2bfa0833efc879010b5bbfdbb1f to your computer and use it in GitHub Desktop.
Save felipecsl/8925b2bfa0833efc879010b5bbfdbb1f to your computer and use it in GitHub Desktop.
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