Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dashsaurabh/41c49e0d5771943c18668d46ce47925c to your computer and use it in GitHub Desktop.
Save dashsaurabh/41c49e0d5771943c18668d46ce47925c to your computer and use it in GitHub Desktop.
Find the Length of the Longest Substring without Duplicates
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