Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Last active April 19, 2017 20:07
Show Gist options
  • Save animatedlew/49cadff712e016e080927e26d300af31 to your computer and use it in GitHub Desktop.
Save animatedlew/49cadff712e016e080927e26d300af31 to your computer and use it in GitHub Desktop.
let S = "ADOBECODEBANC";
let T = "ABC";
let cursors = {};
// init cursors
for (let t of T) cursors[t] = -1;
// O(n) time complexity
S.split('').forEach((s, i) => {
if (s in cursors && i >= cursors[s])
cursors[s] = i;
});
// grab all values
let indices = Object.values(cursors);
// present window
let w = {
s: Math.min.apply(null, indices),
e: Math.max.apply(null, indices)
};
const valueInWindow = S.slice(w.s, w.e+1);
console.log(valueInWindow);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment