Skip to content

Instantly share code, notes, and snippets.

@AndrewThian
Last active February 9, 2019 05:23
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 AndrewThian/593e507c3f9175436a61bd180b1a6a70 to your computer and use it in GitHub Desktop.
Save AndrewThian/593e507c3f9175436a61bd180b1a6a70 to your computer and use it in GitHub Desktop.
js challenge for asserting substrings
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