String Algorithms and Pattern Matching

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

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

Fetch the same bytes

The static files are identical to what the API returns, but with no rate limit and no server round trip. Use the API when you want a query and a content type; use the files when you want to cache one document.

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