Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save lienista/8d338ebf1f609ede60f7d772d279f905 to your computer and use it in GitHub Desktop.

Select an option

Save lienista/8d338ebf1f609ede60f7d772d279f905 to your computer and use it in GitHub Desktop.
Algorithms in Javascript: Leetcode 3. Longest Substring Without Repeating Characters - Given a string, find the length of the longest substring without repeating characters.
const lengthOfLongestSubstring = (s) => {
if(s === '') return 0;
let a = s.split('');
let newS = '';
let len = 1;
let words =a[0];
let sLength = a.length;
for(let i=0; i<sLength; i++){
newS = a[i];
let wordLen = sLength;
for(let j=i+1; j < wordLen; j++){
if(newS.indexOf(a[j]) <0){
newS += a[j];
} else {
break; //restart new word
}
}
if(words.indexOf(newS) <0 && len < newS.length) {
words = newS;
len = newS.length;
}
}
return len;
}
console.log(lengthOfLongestSubstring('abcabcbb'));
console.log(lengthOfLongestSubstring('bbbbb'));
console.log(lengthOfLongestSubstring('pwwkew'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment