Created
September 6, 2019 03:39
-
-
Save masautt/8cb604746c7dff090d0654759d1e19a7 to your computer and use it in GitHub Desktop.
How to find longest substring without repeating characters in JavaScript?
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 => { | |
let longest = 0; | |
let start = 0; | |
const seen = {}; | |
[...s].forEach((char, i) => { | |
if (char in seen && start <= seen[char]) { | |
longest = Math.max(i - start, longest); | |
start = seen[char] + 1; | |
} | |
seen[char] = i; | |
}); | |
return Math.max(s.length - start, longest); | |
}; | |
//https://codereview.stackexchange.com/questions/220228/longest-substring-without-repeating-characters-in-js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment