Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.