Knuth-Morris-Pratt

Pattern-matching algorithm with pattern preprocessing and linear runtime performance. Works better on shorter patterns.

indexOfKMP()

/**
 * Finds pattern enclosure in the source string using Knuth-Morris-Pratt algorithm.
 *
 * @param source Source string.
 * @param pattern Pattern as string or precomputed pattern.
 * @returns The leftmost index at which the pattern begins in the source string or -1.
 */
function indexOfKMP(source: string, pattern: string | PrecomputedPattern): number

precomputePattern()

/**
 * Precomputes pattern overlaps between front and tail character sequences.
 *
 * @param pattern Pattern string.
 * @returns Precomputed pattern.
 */
function precomputePattern(pattern: string): PrecomputedPattern

Last updated

Was this helpful?