Computing Atlas

How Computing Was Built
Sign In
Text size
100%
Theme
Algorithm

Two-way string-matching algorithm

String Algorithm

The two-way string-matching algorithm, also called the Crochemore-Perrin string-matching algorithm, is a string-searching algorithm discovered by Maxime Crochemore and Dominique Perrin in 1991. It searches for copies of a short needle of length m inside a longer haystack of length n. After preprocessing the needle in time proportional to m, the search itself takes time linear in the length of the haystack. It can be viewed as a combination of the forward-going Knuth-Morris-Pratt algorithm and the backward-running Boyer-Moore algorithm: like them it preprocesses the needle to find partially repeating periods and uses them to compute shifts, but unlike them it needs only constant additional space for that information. The preprocessing computes the period of the needle and a critical factorization of it into a prefix and a suffix, and the matching phase performs at most 2n minus m comparisons. Breslauer later published two variants that make fewer comparisons at the cost of storing extra offsets. The algorithm is cache-friendly and is used by the C standard libraries glibc, newlib and musl to implement the memmem and strstr family of functions.

Facts
Time Complexity
Time Complexity (category)
Linear Time -- O(n) 1
Space Complexity
Constant 1
Sources
1. Two-way string-matching algorithm (Wikipedia)
  • Lead, search time
    the actual search takes time O(n) linear in the length of the haystack
  • Lead, extra space
    it uses only O(1) additional space to store information about those partial repeats
  • In Group: String Searching Algorithms, Lead
    is a string-searching algorithm, discovered by Maxime Crochemore and Dominique Perrin in 1991
View the Source
Comments (0)
No comments yet. Be the first to share a thought.
Reader Challenges (0)
No disputes yet. Spotted an error or a better source? Open the first one.