String matching in Lempel-Ziv compressed strings

M. Farach, M. Thorup

    Research output: Contribution to journalArticlepeer-review


    String matching and compression are two widely studied areas of computer science. The theory of string matching has a long association with compression algorithms. Data structures from string matching can be used to derive fast implementations of many important compression schemes, most notably the Lempel-Ziv (LZ77) algorithm. Intuitively, once a string has been compressed - and therefore its repetitive nature has been elucidated - one might be tempted to exploit this knowledge to speed up string matching. The Compressed Matching Problem is that of performing string matching in a compressed text, without uncompressing it. More formally, let script T sign be a text, let Z be the compressed string representing script T sign, and let P be a pattern. The Compressed Matching Problem is that of deciding if P occurs in script T sign, given only P and Z. Compressed matching algorithms have been given for several compression schemes such as LZW. In this paper we give the first nontrivial compressed matching algorithm for the classic adaptive compression scheme, the LZ77 algorithm. In practice, the LZ77 algorithm is known to compress more than other dictionary compression schemes, such as LZ78 and LZW, though for strings with constant per bit entropy, all these schemes compress optimally in the limit. However, for strings with o(1) per bit entropy, while it was recently shown that the LZ77 gives compression to within a constant factor of optimal, schemes such as LZ78 and LZW may deviate from optimality by an exponential factor. Asymptotically, compressed matching is only relevant if |Z| = o(|T|), i.e., if the compression ratio |script T sign|/|Z| is more than a constant. These results show that LZ77 is the appropriate compression method in such settings. We present an LZ77 compressed matching algorithm which runs in time O(N log2 U/N+P) where N = |Z|, U = |script T sign|, and P = |P|. Compare with the naïve "decompresion" algorithm, which takes time Θ(U + P) to decide if P occurs in script T sign. Writing U + P as N · U/N + P, we see that we have improved the complexity, replacing the compression factor U/N by a factor log2 U/N. Our algorithm is competitive in the sense that O (N log2 U/N + P) = O (U + P), and opportunistic in the sense that O (N log2 U/N + P) = o(U + P) if N = o(U) and P = o(U).

    Original languageEnglish (US)
    Pages (from-to)388-404
    Number of pages17
    JournalAlgorithmica (New York)
    Issue number4
    StatePublished - 1998


    • Algorithms
    • Compression
    • String matching

    ASJC Scopus subject areas

    • General Computer Science
    • Computer Science Applications
    • Applied Mathematics


    Dive into the research topics of 'String matching in Lempel-Ziv compressed strings'. Together they form a unique fingerprint.

    Cite this