Skip to content

Instantly share code, notes, and snippets.

@roger35972134
Created October 11, 2017 07:28
Show Gist options
  • Save roger35972134/bf31433738d37bb618e4bf2b8a32cc00 to your computer and use it in GitHub Desktop.
Save roger35972134/bf31433738d37bb618e4bf2b8a32cc00 to your computer and use it in GitHub Desktop.
Finally
def length_of_longest_substring(s)
max = 0
start_index = -1
char_to_index_map = {}
s.chars.each_with_index do |c, i|
if current_index = char_to_index_map[c] and current_index > start_index # if current_index != nil
start_index = current_index
end
char_to_index_map[c] = i
new_length = i - start_index
max = new_length if max < new_length
end
max
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment