Created
July 24, 2023 08:46
-
-
Save turbod/cdca36d5d8cfaee7fec7f7e4a283159b to your computer and use it in GitHub Desktop.
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
# 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