Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jacky810124/565decc0c50c1f232b1fbe6ce20012bb to your computer and use it in GitHub Desktop.
Save jacky810124/565decc0c50c1f232b1fbe6ce20012bb to your computer and use it in GitHub Desktop.
Longest Substring Without Repeating Characters
var lengthOfLongestSubstring = function(s) {
var start = 0
var end = 0
var temp = ''
var result = ''
var times = 1
while (end < s.length) {
console.log(`#${times}`)
var findLastChar = function(char) {
return s.slice(0, end).lastIndexOf(char)
}
var selectedChar = s.slice(end, end + 1)
if (temp.indexOf(selectedChar) >= 0) {
start = findLastChar(selectedChar) + 1
temp = s.slice(start, end + 1)
} else {
temp += selectedChar
}
if (result.length < temp.length) {
result = temp
}
console.log(`result: ${result}`)
console.log(`temp: ${temp}`)
console.log(`start: ${start}, end: ${end}`)
console.log(`selected char: ${selectedChar}`)
console.log('-------------------')
end ++
times ++
}
console.log(result)
return result.length
}
lengthOfLongestSubstring('dvdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment