KMP Substring Search With Prefix Table
Build the KMP prefix table and report every overlapping match index in linear time, including empty-pattern and Unicode code unit behavior.
Code
function assertString(value: unknown, name: string): asserts value is string {
if (typeof value !== "string") throw new TypeError(name + " must be a string");
}
export function buildPrefixTable(pattern: string): number[] {
assertString(pattern, "pattern");
const table = new Array<number>(pattern.length).fill(0);
let length = 0;
for (let index = 1; index < pattern.length;) {
if (pattern[index] === pattern[length]) {
length += 1;
table[index] = length;
index += 1;
} else if (length > 0) {
length = table[length - 1];
} else {
table[index] = 0;
index += 1;
}
}
return table;
}
export function searchAll(text: string, pattern: string): number[] {
assertString(text, "text");
assertString(pattern, "pattern");
if (pattern.length === 0) {
return Array.from({ length: text.length + 1 }, (_, index) => index);
}
const table = buildPrefixTable(pattern);
const matches: number[] = [];
let matched = 0;
for (let index = 0; index < text.length; index += 1) {
while (matched > 0 && text[index] !== pattern[matched]) {
matched = table[matched - 1];
}
if (text[index] === pattern[matched]) matched += 1;
if (matched === pattern.length) {
matches.push(index - matched + 1);
matched = table[matched - 1];
}
}
return matches;
}How the snippet works
KMP Substring Search With Prefix Table keeps everything in the functions assertString, buildPrefixTable and searchAll.
The nesting pairs for (let index = 1; index < pattern.length;) with for (let index = 0; index < text.length; index += 1) — the inner runs once per outer step. The code refuses to proceed on bad state and throws TypeError at its guard.
What to watch for
- The guard
typeof value !== "string"short-circuits the flow with an earlythrowwhen its condition holds.
Applying it
The primary signature is export function buildPrefixTable(pattern: string)
The surface worth importing is buildPrefixTable; the helpers around it are private to the snippet.
Statements that matter
return Array.from({ length: text.length + 1 }, (_, index) => index);
if (typeof value !== "string") throw new TypeError(name + " must be a string");
if (text[index] === pattern[matched]) matched += 1;
Interactive Tools for This Topic
Big-O Complexity VisualizerVisualize and compare algorithm time complexities like O(1), O(log n), O(n), O(n log n), and O(n²). Plot growth curves to understand performance scaling at a glance.Fuzzy Match TesterTest subsequence fuzzy matching: check whether pattern characters appear in order inside a text, with positions and highlighting.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- How does the code behave when input is bad?
- Validation comes first — `TypeError` marks the failure at `typeof value !== "string"`, before any partial work happens.
- What has to be installed first?
- Nothing — the runtime itself provides everything the code touches. The file defines `assertString`, `buildPrefixTable`, `searchAll` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.