Skip to content

Instantly share code, notes, and snippets.

@akinjide
Created January 24, 2020 09:10
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 akinjide/58956a13ca27cf0775fe33b35bd87cfa to your computer and use it in GitHub Desktop.
Save akinjide/58956a13ca27cf0775fe33b35bd87cfa to your computer and use it in GitHub Desktop.
/**
Find the longest substring in string. Output the length of the substring
e.g
s = 'ababc'
sub = 3
*/
const s = 'ababc';
const n = s.length;
const map = {};
let sub = 0;
for (let j = 0, i = 0; j < n; j++) {
if (map[s[j]]) {
i = Math.max(map[s[j]], i)
}
sub = Math.max(j - i + 1, sub)
map[s[j]] = j + 1
}
console.log(sub)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment