String algorithms are the computational methods for processing and analyzing sequences of symbols drawn from a finite alphabet. The objects of study are strings—ordered, finite sequences such as words in a text, nucleotides in a genome, or bytes in a file—and the central questions concern how to search, compare, align, index, and transform them efficiently. Because strings are one of the most universal data representations, these algorithms underpin text editors, web search, bioinformatics, data compression, and natural language processing. The field is defined less by a single technique than by a shared commitment to exploiting the structure of sequences to avoid brute-force scanning.
The foundational problem is exact pattern matching: given a text of length n and a pattern of length m, find all occurrences of the pattern in the text. The naive approach—checking the pattern at every position—takes O(nm) time in the worst case. The field's early achievements were algorithms that solve this in O(n + m) time, meaning the work grows only linearly with the input size. The best-known are the Knuth–Morris–Pratt algorithm, which preprocesses the pattern to build a failure function that tells how far to shift after a mismatch, and the Boyer–Moore algorithm, which scans the pattern from right to left and uses two heuristics to skip large portions of the text. Both remain influential, though for different reasons: Knuth–Morris–Pratt is conceptually clean and worst-case optimal, while Boyer–Moore is often faster in practice on natural-language text.
A second core problem is approximate matching, also called string matching with errors or edit distance. Here the goal is to find occurrences of a pattern that differ by a small number of insertions, deletions, or substitutions. The classic formulation is the Levenshtein distance, which counts the minimum number of such single-character edits to transform one string into another. Dynamic programming solves this in O(nm) time, and the same table can be adapted to find the best local alignment between two strings—a problem central to comparing biological sequences. Approximate matching is fundamentally harder than exact matching because the number of candidate alignments explodes, and much of the field's later work has focused on heuristic speedups, such as filtering out regions that cannot possibly contain a match, or using bit-parallel techniques that pack several operations into a single machine word.
A third problem is string indexing. Instead of answering one query at a time, an index preprocesses the text once so that many future queries can be answered quickly. The suffix tree, a compressed trie of all suffixes of a text, supports pattern matching in O(m) time regardless of text length, and also enables finding repeated substrings, longest common substrings, and other combinatorial queries. The suffix array, a sorted list of all suffixes, achieves the same query time with much less memory, and the Burrows–Wheeler transform, originally developed for compression, provides an even more compact representation that supports backward search. These three structures are deeply related: the suffix array can be built from the suffix tree, and the Burrows–Wheeler transform can be derived from the suffix array. Modern indexes, such as the FM-index, combine the Burrows–Wheeler transform with auxiliary data to support pattern matching in space proportional to the compressed text itself, which is essential for indexing whole genomes.
The earliest systematic work on strings came from combinatorics on words, a branch of mathematics that studies the structure of sequences without reference to any particular application. This tradition, associated with figures like Axel Thue in the early twentieth century, produced results about repetitions, periods, and avoidable patterns that later became tools for algorithm designers. A string has a period if it can be written as a prefix repeated some number of times; the Fine–Wilf theorem bounds how long two different periods can coexist. The concept of a border—a prefix that is also a suffix—underlies the failure function of Knuth–Morris–Pratt. The Z-algorithm, which computes for each position the length of the longest substring starting there that matches a prefix of the string, is a direct algorithmic descendant of this combinatorial viewpoint.
This tradition's contribution is not a set of algorithms so much as a way of seeing strings as mathematical objects with hidden regularities. The insight that a string's internal repetitions can be exploited to skip work is the single most important idea in the field. It appears in the preprocessing of patterns, in the construction of suffix arrays, and in the analysis of runs—maximal repetitions—which has been a rich area of recent research. The combinatorial tradition also produced the concept of Lyndon words, strings that are strictly smaller than all their nontrivial rotations, which turn out to be useful for factoring strings and for constructing suffix arrays in linear time.
A second major approach treats strings as data to be processed with the full toolkit of algorithm design: divide and conquer, dynamic programming, hashing, and randomized methods. This tradition is less concerned with the mathematical structure of strings per se and more with achieving practical efficiency on large inputs. Rabin–Karp is the canonical example: it hashes every substring of length m and compares hash values, using a rolling hash to update in constant time per position. The method is randomized—hash collisions can produce false positives—but with a good hash function the probability of error is negligible, and the algorithm is simple and parallelizable.
Suffix array construction illustrates the divide-and-conquer approach. The skew algorithm, also called the DC3 algorithm, constructs a suffix array in linear time by recursively sorting a subset of suffixes and then merging. This is a purely algorithmic achievement, independent of any deep combinatorial property of strings. Similarly, Burrows–Wheeler transform construction can be done by sorting rotations, which is itself a string problem that has benefited from both combinatorial insights and engineering improvements.
The algorithmic engineering tradition also includes bit-parallelism, a technique that represents the state of a dynamic programming computation as bits in a machine word and updates all entries simultaneously using bitwise operations. The Shift-And algorithm for exact matching and the Myers algorithm for approximate matching both use this idea, achieving speedups of a factor equal to the word size—typically 64—over naive dynamic programming. This approach is particularly valuable for approximate matching, where the O(nm) dynamic programming table would otherwise be prohibitively slow for long strings.
A third approach, which emerged later and has become dominant in large-scale applications, treats strings as objects to be compressed and indexed simultaneously. The key realization is that a compressed representation of a string can support queries without being decompressed first. The Burrows–Wheeler transform is the cornerstone: it permutes the characters of a string so that repeated substrings cluster together, making the result highly compressible by simple methods like run-length encoding or move-to-front coding. But the transform has an additional property: it is reversible, and the reversal process can be adapted to support pattern matching.
The FM-index, introduced by Ferragina and Manzini, combines the Burrows–Wheeler transform with a small set of auxiliary arrays to answer pattern-matching queries in O(m) time, using space proportional to the compressed text. The key operation is backward search: starting from the end of the pattern, each character narrows the range of suffixes that could match, using a function that counts occurrences of characters in the transform. This is a genuinely different paradigm from the earlier suffix tree and suffix array approaches, because it never materializes the full index structure. The FM-index made it possible to index texts of billions of characters—entire genomes—on a single machine, which was previously infeasible.
This tradition also includes grammar-based compression, where a string is represented as a context-free grammar that generates exactly that string, and Lempel–Ziv compression, which parses the string into phrases that are copies of earlier substrings. Both have been adapted to support pattern matching and other queries directly on the compressed representation. The relationship between compression and indexing is now a mature subfield in its own right, with a rich theory of compressed data structures that can answer not only pattern matching but also more general queries, such as counting distinct substrings or finding the longest repeated substring, in time proportional to the compressed size rather than the original length.
A fourth approach focuses on comparing two or more strings to find their similarities and differences. This tradition has its roots in sequence alignment, developed independently in computational biology and in text processing. The edit distance between two strings is the minimum number of insertions, deletions, and substitutions needed to transform one into the other, and the dynamic programming algorithm for computing it is one of the most widely used algorithms in all of computer science. The Needleman–Wunsch algorithm computes a global alignment—an alignment of the entire lengths of both strings—while the Smith–Waterman algorithm computes a local alignment, finding the most similar substring pair. These algorithms are O(nm) in time and space, which is acceptable for comparing genes but not for comparing whole genomes.
The alignment tradition has developed several strategies to cope with large inputs. Heuristic aligners, such as BLAST, break the query into short seeds, find exact matches of those seeds in the database, and then extend the matches into full alignments. This trades guaranteed optimality for speed, and it is the standard approach in biological sequence search. Band-dynamic programming restricts the dynamic programming computation to a diagonal band around the main diagonal, which is justified when the two strings are known to be similar. Hirschberg's algorithm reduces the space requirement from O(nm) to O(n) by a divide-and-conquer trick that computes the alignment in two passes, at the cost of doubling the time. The Four Russians technique, which precomputes blocks of the dynamic programming table, achieves a subquadratic time bound for edit distance, though the constant factors make it impractical for most inputs.
This tradition also includes multiple sequence alignment, where the goal is to align three or more strings simultaneously. This problem is NP-hard in general, so practical tools use progressive alignment—building the alignment incrementally by adding one sequence at a time according to a guide tree—or iterative refinement. The field has developed a rich set of scoring schemes, gap penalties, and heuristics, but the underlying algorithmic ideas remain those of dynamic programming and greedy approximation.
A fifth approach considers the setting where the text arrives incrementally, character by character, and queries must be answered before the entire text is seen. This is the online or streaming model. The classic problem is online pattern matching: given a pattern, report every occurrence as soon as the last character of the occurrence has arrived. The Knuth–Morris–Pratt algorithm is naturally online, since it processes the text left to right and maintains a state that encodes how much of the pattern has been matched so far. The Aho–Corasick algorithm generalizes this to multiple patterns simultaneously, building a finite automaton that recognizes all patterns in a single pass over the text.
Streaming algorithms for other string problems are more challenging. Computing the edit distance between a stream and a fixed pattern requires maintaining a column of the dynamic programming table, which can be done in O(m) space and O(m) time per character. Finding the longest repeated substring in a stream is harder, and the best algorithms use a combination of hashing and sampling. The streaming model has become more important with the rise of network monitoring, where data arrives as a continuous stream and cannot be stored in full, and with the processing of massive text corpora that exceed available memory.
The field today is characterized by a convergence of these traditions. The suffix array, once a purely combinatorial structure, is now built using algorithms that combine divide-and-conquer with the Burrows–Wheeler transform. The FM-index, originally a compression technique, is the backbone of modern read aligners in bioinformatics, which map millions of short DNA sequences to a reference genome. The alignment tradition has merged with the indexing tradition in tools like BWA and Bowtie, which use the FM-index to find candidate locations and then use banded dynamic programming to refine the alignment.
The most active research areas reflect this synthesis. String processing on compressed texts asks which queries can be answered in time proportional to the compressed size rather than the original length. Approximate pattern matching with wildcards and gaps generalizes the classical problem to more expressive patterns. Parameterized matching treats two strings as equivalent if one can be transformed into the other by a consistent renaming of characters, which is useful in code clone detection and image processing. Order-preserving matching looks for substrings that have the same relative order of characters, which is relevant in time-series analysis.
A notable recent development is the r-index, which supports pattern matching on highly repetitive texts—such as versioned document collections or pangenome references—in space proportional to the number of runs in the Burrows–Wheeler transform rather than the text length. This is part of a broader effort to handle the growing volume of repetitive data, where traditional indexes are wasteful because they store information about every position even though most positions are nearly identical to others.
The field also maintains a strong theoretical component. The string periodicity lemma, the LCP (longest common prefix) array, and the suffix automaton—a minimal deterministic finite automaton that recognizes all substrings of a string—are objects of ongoing study. The suffix automaton, in particular, is a remarkable structure: it has at most 2m states for a string of length m, and it can be built in linear time, yet it answers many substring queries in constant time per query. It is less well known than the suffix tree but is increasingly recognized as a fundamental tool.
Throughout its history, string algorithms have been shaped by applications as much as by theory. The need to search text led to the early pattern-matching algorithms. The need to compare biological sequences drove the development of alignment algorithms. The need to index massive datasets drove the development of compressed indexes. Each application has contributed new problems, and each new algorithmic idea has found applications beyond the one that motivated it. The field's enduring character is this interplay between the combinatorial structure of strings and the practical demands of processing them at scale.