Skip to content

Instantly share code, notes, and snippets.

@uztbt
Created July 25, 2021 13:34
Show Gist options
  • Save uztbt/9f27c3c08854ae1f3dc2ae9542fd6f8f to your computer and use it in GitHub Desktop.
Save uztbt/9f27c3c08854ae1f3dc2ae9542fd6f8f to your computer and use it in GitHub Desktop.
function createLPSArray(w: string): number[] {
const lps: number[] = [0];
let len = 0;
let i = 1;
while (i < w.length) {
if (w.charAt(i) === w.charAt(len)) {
lps.push(++len);
i++;
} else {
if (len !== 0) len = lps[len-1]; // This is the big point!
else lps[i++] = 0;
}
}
return lps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment