Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TomLisankie/40930edbd4636095cd9a7f37a5efe791 to your computer and use it in GitHub Desktop.
Save TomLisankie/40930edbd4636095cd9a7f37a5efe791 to your computer and use it in GitHub Desktop.
2 ways of solving "Longest Substring Without Repeating Characters" problem in JavaScript
//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