Created
January 19, 2020 11:49
-
-
Save dashsaurabh/41c49e0d5771943c18668d46ce47925c to your computer and use it in GitHub Desktop.
Find the Length of the Longest Substring without Duplicates
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
var lengthOfLongestSubstring = function(s) { | |
let visited = {}; | |
let maximum = 0; | |
let i = 0; | |
for(let j=0;j<s.length;j++) { | |
if(visited[s.charAt(j)] !== undefined) { | |
i = Math.max(i, visited[s.charAt(j)]) | |
} | |
maximum = Math.max(maximum, (j-i)+1) | |
visited[s.charAt(j)] = j+1; | |
} | |
return maximum; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment