Created
July 25, 2021 13:34
-
-
Save uztbt/9f27c3c08854ae1f3dc2ae9542fd6f8f to your computer and use it in GitHub Desktop.
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 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