Skip to content

Instantly share code, notes, and snippets.

@gesslar
Created August 23, 2020 18:54
Show Gist options
  • Save gesslar/134a6419788f3df07aac34b2d0c1c81a to your computer and use it in GitHub Desktop.
Save gesslar/134a6419788f3df07aac34b2d0c1c81a to your computer and use it in GitHub Desktop.
/**
* @param {string} s
* @return {number}
*/
const lengthOfLongestSubstring = s => {
let begin = 0
let end = 1
let final = ""
const length = s.length + 1
while(begin < length && end < length) {
const sub = s.slice(begin, end)
const sublen = sub.length
const test = sub.slice(0, sublen - 1)
const last = sub.slice(sublen - 1)
if(test.indexOf(last) > -1 ) {
begin++
} else {
if(sub.length > final.length) final = sub
end++
}
}
return final.length
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment