Created
October 11, 2017 07:28
-
-
Save roger35972134/bf31433738d37bb618e4bf2b8a32cc00 to your computer and use it in GitHub Desktop.
Finally
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(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