Forked from tombaranowicz/longestSubstringWithoutRepeatingCharacters.js
Last active
January 15, 2020 03:12
-
-
Save TomLisankie/40930edbd4636095cd9a7f37a5efe791 to your computer and use it in GitHub Desktop.
2 ways of solving "Longest Substring Without Repeating Characters" problem in JavaScript
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 METHOD | |
var lengthOfLongestSubstring = function(s) { | |
let count = 0; | |
let i = 0; | |
let j = 0; | |
let end = s.length; | |
let set = new Set(); | |
while (i < end && j < end) { | |
let char = s.charAt(j); | |
if(!set.has(char)) { | |
set.add(char); | |
j++; | |
count = Math.max (count, j - i); | |
} else { | |
set.delete(s.charAt(i)); | |
i++; | |
} | |
} | |
return count; | |
}; | |
let result = lengthOfLongestSubstring('abcabcbb') | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment