Last active
February 9, 2019 05:23
-
-
Save AndrewThian/593e507c3f9175436a61bd180b1a6a70 to your computer and use it in GitHub Desktop.
js challenge for asserting substrings
This file contains 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
function substring(str) { | |
if (str.length <= 1) { | |
return str.length | |
} | |
let startpoint = 0; | |
let max = 0; | |
let len = str.length; | |
let lookup = new Map(); | |
let dictionary = new Map(); | |
for (let i = 0; i < len; i++) { | |
let char = str.charAt(i) | |
if (lookup.has(char) && lookup.get(char) >= startpoint) { | |
const nextPointer = lookup.get(char) + 1 | |
startpoint = nextPointer | |
} | |
lookup.set(char, i) | |
const endpoint = i | |
const difference = (endpoint - startpoint) + 1 | |
const slicedString = str.slice(startpoint, endpoint + 1) | |
dictionary.set(difference, slicedString) | |
max = Math.max(max, difference) | |
} | |
console.log(dictionary.get(max)) | |
console.log(max) | |
} | |
// substring("ghostbusters") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment