String Algorithms and Pattern Matching

String processing algorithms: KMP, Rabin-Karp, Z-algorithm, suffix trees/arrays, Aho-Corasick, and edit distance (Levenshtein).

  • JSON7,830 B
  • YAML5,394 B · −31%
  • TOON4,755 B · −39%

String algorithms and the structures behind them: KMP, Rabin-Karp, the Z-algorithm, suffix trees and arrays, Aho-Corasick and Levenshtein edit distance, each with type, time, space, description, uses and notes.

Use it when

  • You need pattern matching, multi-pattern search or string similarity.
  • You are choosing between algorithms for text search.

Watch out for

  • Complexities assume a fixed alphabet or a stated alphabet size; check the notes.
  • For ordinary text search, a library routine is usually the right first choice.

The data

Algorithms

NameTypeTimeSpaceDescriptionUse casesNotesBuild time
Knuth-Morris-Pratt (KMP)Pattern MatchingO(n + m) preprocessing + searchO(m)Uses failure function (longest proper prefix that is also suffix) to skip redundant comparisons
  • Text search in editors
  • DNA sequence matching
  • Intrusion detection (signature matching)
Preprocessing builds π (pi) table; search never backtracks in textnull
Rabin-KarpPattern Matching (Rolling Hash)O(n + m) average, O(nm) worstO(1)Uses hash of pattern and rolling hash of text windows; matches hash before character-by-character verify
  • Plagiarism detection
  • Multiple pattern matching
  • Fingerprinting
  • Duplicate detection
Extendable to 2D pattern matching; collision probability managed with double hashingnull
Z-AlgorithmPattern MatchingO(n + m)O(n + m)Computes Z-array: length of longest substring starting at each position that matches a prefix
  • String matching
  • Period detection
  • Longest common prefix queries
Concatenate pattern$text with sentinel; Z-values ≥ pattern length indicate matchesnull
Suffix TreeString IndexnullO(n)Compressed trie of all suffixes; enables O(m) pattern search, longest repeated substring, etc.
  • Genomic sequence analysis
  • Full-text search
  • Longest common substring
  • Palindrome detection
Ukkonen's algorithm builds in linear time; high constant factor and memory usage in practiceO(n)
Suffix ArrayString IndexnullO(n)Sorted array of all suffixes; with LCP array enables most suffix tree operations with less memory
  • Compressed text indexing
  • Bioinformatics
  • Data compression (BWT)
Binary search for pattern in O(m log n); with LCP array can achieve O(m + log n)O(n log n) or O(n)
Aho-CorasickMulti-pattern MatchingO(n + m + z) where z = number of matchesO(m)Builds automaton from dictionary of patterns; finds all occurrences of all patterns in one text pass
  • Virus/malware signature scanning
  • Keyword highlighting
  • Content filtering
  • Aho-Corasick automaton
Extends KMP failure function to a trie; failure links point to longest proper suffix that is a dictionary prefixnull
Levenshtein Distance (Edit Distance)String SimilarityO(m * n)O(min(m,n)) with optimizationMinimum number of single-character edits (insert, delete, substitute) to transform one string into another
  • Spell checking
  • Fuzzy search
  • DNA sequence alignment
  • Plagiarism detection
  • OCR error correction
Dynamic programming; can be optimized with Ukkonen's cutoff for bounded distance queriesnull

Fields

The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.

SectionFieldTypeExample
algorithmsnamestringKnuth-Morris-Pratt (KMP)
algorithmstypestringPattern Matching
algorithmstimestringO(n + m) preprocessing + search
algorithmsspacestringO(m)
algorithmsdescriptionstringUses failure function (longest proper prefix th…
algorithmsuse_casesarray[3 items]
algorithmsnotesstringPreprocessing builds π (pi) table; search never…
algorithmsbuild_timestringO(n)

Fetch the same bytes

The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.

curl "https://yjtoon.com/api/dataset/string-algorithms?format=toon"
const res = await fetch(
  "https://yjtoon.com/static-data/dataset/string-algorithms.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • strings
  • pattern-matching
  • kmp
  • rabin-karp
  • z-algorithm
  • suffix-tree
  • aho-corasick
  • levenshtein

Search the library

Search by a topic, title, or keyword.

Start with a collection

Browse all 205 datasets