Last active
October 11, 2018 22:59
-
-
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.
This file contains hidden or 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
| 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