Skip to content
CoreSource checked

Morris-Pratt

Morris-Pratt uses using prefix borders to avoid rechecking matched characters for exact string matching.

Quick Facts

Compact metadata for implementation and discovery.

Category
Pattern Matching
Difficulty
Intermediate
Family
String Matching
Tier
Core
Status
Verified
Review
Source Checked
Sources
2
Formulas
0
Relationships
3
Code example
Ready
Visualization
Ready

Overview

Morris-Pratt handles exact string matching by using prefix borders to avoid rechecking matched characters.

Core Concepts

exact string matchingstring matchingmatchingpattern matching

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n + m)

Worst

O(n + m)

Space

O(n + m)

When to Use

  • Use Morris-Pratt when solving string matching and you want to benefit from using prefix borders to avoid rechecking matched characters.
  • Choose it when O(n + m) average/worst-case time and O(n + m) memory match your workload profile.
  • Prefer it over Bit-Parallel Levenshtein or Shift-And Algorithm when the problem structure favors exact string matching.

When Not to Use

  • Avoid when preprocessing cost is larger than the savings for a one-off tiny input.
  • Avoid when the alphabet or scoring assumptions do not match the sequence data.

What It Does

  • Morris-Pratt uses using prefix borders to avoid rechecking matched characters for exact string matching
  • Organizes the work around exact string matching and string matching.

Use Cases

  • Production and research workloads requiring string matching via using prefix borders to avoid rechecking matched characters.
  • String Matching pipelines where O(n + m) time and O(n + m) space fit system constraints.
  • Engineering comparisons against Bit-Parallel Levenshtein and Shift-And Algorithm when tuning string matching.

Inputs

  • Text or sequence
  • Pattern, index, or comparison sequence

Outputs

  • Matches, alignment, index, or similarity value

Pseudocode

  1. 1.Preprocess the input alphabet, pattern/grammar rules, or symbol frequency tables for Morris-Pratt.
  2. 2.Construct the lookup automaton, suffix/shift index, or coding state (exact string matching, string matching).
  3. 3.Scan the input sequence by using prefix borders to avoid rechecking matched characters.
  4. 4.Emit matches, derivation nodes, or encoded tokens while advancing the state transition table.
  5. 5.Finalize trailing state and return the completed string matching output.

Canonical Example

Input
Text or sequence configured for a representative string matching test case
Output
Matches, alignment, index, or similarity value satisfying Morris-Pratt invariants
Explanation
Morris-Pratt processes the input by using prefix borders to avoid rechecking matched characters, tracking exact string matching and string matching to produce the verified string matching result.

Implementation Notes

  • Keep preprocessing output inspectable with short examples.
  • Test repeated characters and overlapping matches.
  • Document sentinel or alphabet assumptions.

Common Pitfalls

  • Mishandling empty patterns, repeated characters, or sentinel symbols.
  • Confusing preprocessing indexes with the query algorithm.
  • Forgetting whether indexing is zero-based or one-based.

Interactive Mechanism Walkthrough

Sequence Window & Automaton Alignment

Show using prefix borders to avoid rechecking matched characters on a small exact string matching example.

Input:ABABABCABAB
Window:ABAB
Step 1 of 5: Preprocess the input alphabet, pattern/grammar rules, or symbol frequency tables for Morris-Pratt.
pattern-matching-mechanism

Notes

Morris-Pratt operates in O(n + m) time and O(n + m) space within the String Matching family. Often compared with Bit-Parallel Levenshtein and Shift-And Algorithm.

Sources (2)

What each reference supports for this entry.

Algorithms on Strings, Trees, and Sequences

Book

String algorithms reference for Morris-Pratt's pattern matching, suffix indexing, alignment, or sequence-processing mechanism.

Open source

Introduction to Algorithms (CLRS)

Book

Textbook support for Morris-Pratt's algorithm family, complexity framing, and standard implementation concepts.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Both address exact string matching, but they make different modeling or performance tradeoffs.

Alternative toShift-And Algorithm

Both address exact string matching, but they make different modeling or performance tradeoffs.

Alternative toShift-Or Algorithm

Both address exact string matching, but they make different modeling or performance tradeoffs.

Tags

Pattern MatchingStringsMatching