Skip to content

Instantly share code, notes, and snippets.

@OlegKorn
Last active November 1, 2022 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlegKorn/a2ac57112e39f2d9ccaceaa3f82bc095 to your computer and use it in GitHub Desktop.
Save OlegKorn/a2ac57112e39f2d9ccaceaa3f82bc095 to your computer and use it in GitHub Desktop.
var lengthOfLongestSubstring = function(s, len=undefined) {
// check if all chars same
if (!len) {
if (s.length !== 0) {
const arr = [...s]
const allEqual = arr => arr.every(val => val === arr[0])
if (allEqual(arr)) {
return 1
}
}
if (s.length === 0) {
return 0
}
if (s.length === 1) {
return 1
}
if (s.length === 2) {
if (s[0] !== s[1]) {
return 2
}
else {
return 1
}
}
}
var sl = s[0]
var sls = []
var tempLength = 0
if (len && len > tempLength) {
tempLength = len
}
for (var i = 1; i < s.length; i++) {
while (!sl.includes(s[i])) {
sl += s[i]
console.log(s, sl)
}
if (sl.includes(s[i+1])) {
s = s.substr(sl.length, s.length)
sl = s[0]
return lengthOfLongestSubstring(s, len=sl.length)
}
}
}
//console.log(lengthOfLongestSubstring("pwwkew"))
//console.log(lengthOfLongestSubstring("bbbbb"))
console.log(lengthOfLongestSubstring("pwwkew"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment