Skip to content

Instantly share code, notes, and snippets.

@turbod
Created July 24, 2023 08:46
Show Gist options
  • Save turbod/cdca36d5d8cfaee7fec7f7e4a283159b to your computer and use it in GitHub Desktop.
Save turbod/cdca36d5d8cfaee7fec7f7e4a283159b to your computer and use it in GitHub Desktop.
# Sliding window algorithm
#
# @param {String} s
# @return {Integer}
def length_of_longest_substring(s)
max = 0
subset = []
s.each_char do |char|
if subset.include?(char)
max = subset.length if max < subset.length
subset = [char]
else
subset << char
end
end
max
end
#puts length_of_longest_substring('abcabacb')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment