Collection Kit

AhoCorasick

String

The Aho-Corasick algorithm is used for matching a set of strings in linear time. It builds a finite state machine that allows for efficient searching of multiple patterns simultaneously.

Import Statement
import { AhoCorasick } from "collection-kit";

Key Features

  • Multi-Pattern Matching: Can search for multiple patterns in a single pass through the text.
  • Linear Time Complexity: Provides O(n + m + z) time complexity, where n is the length of the text, m is the total length of the patterns, and z is the number of matches.

Common Operations

Build
Construct the Aho-Corasick automaton from a set of patterns.
Search
Search for patterns in a given text.

Example Code

import { AhoCorasick } from "collection-kit";

const ac = new AhoCorasick();
ac.build(["he", "she", "his", "hers"]);

const matches = ac.search("ushers");
console.log("Matches:", matches); // ["she", "he", "hers"]