Papers
Topics
Authors
Recent
Search
2000 character limit reached

Edit Distance (Levenshtein)

Updated 5 July 2026
  • Edit Distance is defined as the minimum number of insertions, deletions, and substitutions required to convert one string into another, following the Wagner–Fischer algorithm.
  • Dynamic programming, fine-grained hardness, and approximation schemes provide both exact and near-linear time solutions, addressing challenges like weighted operations and thresholded regimes.
  • Variants extend to geometric sequences, language-level automata, and learned embeddings, underpinning applications in spell correction, sequence alignment, and robust text classification.

Edit distance (ED), also called Levenshtein distance, is the minimum number of insertions, deletions, and substitutions required to transform one string into another. In the standard unit-cost model, ED is a metric: it is nonnegative, symmetric, zero iff the strings are identical, and satisfies triangle inequality. The notion extends beyond plain strings to weighted operations, directed distances between languages, and geometric point-sequence alignment, and it underlies tasks such as spell correction, data de-duplication, sequence alignment, document exchange, sketching, streaming, and robust text classification (Dai et al., 2020, Goldenberg et al., 2022, Chatterjee et al., 2015, Agarwal et al., 2015).

1. Formal definition and core properties

For strings sxs_x and sys_y of lengths MM and NN, the classical Wagner–Fischer recurrence defines the edit distance Δe(sx,sy)\Delta_e(s_x,s_y) by

Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],

with

D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,

and

D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).

The three cases correspond to deletion, insertion, and match/substitution, respectively (Dai et al., 2020). At the word level, the same unit-cost model can be phrased as sequences of operations I(a,i)I(a,i), D(a,i)D(a,i), and sys_y0, and the edit distance sys_y1 is the minimum length of an edit sequence transforming sys_y2 into sys_y3 (Chatterjee et al., 2015).

A directed language-level generalization is

sys_y4

which is asymmetric in general (Chatterjee et al., 2015). A further geometric generalization replaces characters by points sys_y5, matches by Euclidean costs, and gaps by a fixed penalty sys_y6, yielding

sys_y7

over monotone matchings sys_y8 (Agarwal et al., 2015). These formulations preserve the basic dynamic-programming viewpoint while changing the underlying object class or operation costs.

2. Exact computation, fine-grained hardness, and dynamic maintenance

The textbook dynamic program computes ED in sys_y9 time for length-MM0 strings, and for two strings of length MM1 the best known exact algorithms run in about

MM2

time (Dai et al., 2020). Fine-grained lower bounds explain why this quadratic barrier is persistent: there is strong evidence, conditional on SETH, that one cannot do strongly subquadratic exact computation for arbitrary strings (Dai et al., 2020), and exact ED in time MM3 would refute the Strong Exponential Time Hypothesis (Mao et al., 31 Mar 2026).

Weighted variants change the algorithmic landscape but do not eliminate hardness. For the family MM4, where insertions and deletions cost MM5 and substitutions cost MM6, exact bounded-distance computation is possible in

MM7

and this bound is near-optimal assuming the Orthogonal Vectors Conjecture (Goldenberg et al., 2022). For fully weighted edit distance MM8, the standard dynamic program remains MM9, but when a threshold NN0 is fixed in advance, dynamic maintenance becomes possible: one can maintain NN1 with preprocessing

NN2

and update time

NN3

and conditional lower bounds show fine-grained optimality of this trade-off for NN4 (Boneh et al., 3 Jul 2025). This establishes a sharp contrast between unparameterized worst-case computation and bounded-distance or thresholded regimes.

3. Approximation regimes and subquadratic algorithms

Approximation results separate several regimes of ED. For “far” pairs, there are constants NN5 and NN6 such that, for every NN7, a randomized algorithm runs in

NN8

time and outputs an upper bound NN9 satisfying

Δe(sx,sy)\Delta_e(s_x,s_y)0

with high probability; in particular, whenever Δe(sx,sy)\Delta_e(s_x,s_y)1, this is a constant-factor approximation (Koucký et al., 2019). This isolates a regime in which nearly linear time is achievable at the cost of additive slack.

Gap formulations sharpen the sublinear-access picture. In the Δe(sx,sy)\Delta_e(s_x,s_y)2-Gap Edit Distance problem, a non-adaptive randomized algorithm uses

Δe(sx,sy)\Delta_e(s_x,s_y)3

queries, and this query complexity is optimal up to polylogarithmic factors (Goldenberg et al., 2021). The same work shows that for Δe(sx,sy)\Delta_e(s_x,s_y)4 the running time matches this bound, while for Δe(sx,sy)\Delta_e(s_x,s_y)5 the time is

Δe(sx,sy)\Delta_e(s_x,s_y)6

(Goldenberg et al., 2021). This gives a complete non-adaptive query characterization of gap ED.

Most recently, randomized approximation schemes achieve arbitrarily fine approximation in quasi-strongly subquadratic time: for any constant Δe(sx,sy)\Delta_e(s_x,s_y)7, ED admits a Δe(sx,sy)\Delta_e(s_x,s_y)8-approximation in

Δe(sx,sy)\Delta_e(s_x,s_y)9

time (Mao et al., 31 Mar 2026). This yields a separation between the complexity of approximate ED and exact ED even for approximation factor arbitrarily close to Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],0 (Mao et al., 31 Mar 2026).

4. Sketching, streaming, and communication complexity

The small-distance regime admits strong sketching and communication guarantees. In the document exchange problem, a message of size

Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],1

bits suffices for exact recovery when Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],2, with encoding and decoding time Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],3 (Belazzougui et al., 2016). In the referee model, there are sketches of size Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],4 bits that allow exact recovery of ED and the full edit sequence when the distance is at most Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],5; the same work gives the first streaming algorithm for computing ED and all edits exactly using Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],6 bits of space (Belazzougui et al., 2016).

Subsequent work tightened simultaneous sketching complexity. An improved analysis of the Belazzougui–Zhang protocol yields a simultaneous sketch of size

Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],7

for precisely computing Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],8 and an optimal edit sequence under the promise Δe(sx,sy)=D[M,N],\Delta_e(s_x,s_y)=D[M,N],9 (Jin et al., 2020). A different line constructs an almost linear-size sketching scheme for thresholded exact ED: the sketch size is

D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,0

and from two sketches one can recover D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,1 together with an optimal sequence of edit operations when D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,2, and output LARGE otherwise (Koucký et al., 2024).

Streaming lower bounds show that ED remains qualitatively harder than Hamming distance even when one string is fully random-access. In the asymmetric streaming model, any D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,3-pass randomized algorithm deciding whether D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,4 must use

D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,5

space, exact ED needs D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,6 space, and this yields an exponential separation between edit distance and Hamming distance in that model (Li et al., 2021). On the algorithmic side, if D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,7, there are one-pass deterministic algorithms using

D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,8

space for a D[0,0]=0,D[i,0]=i,D[0,j]=j,D[0,0]=0,\quad D[i,0]=i,\quad D[0,j]=j,9-approximation and

D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).0

space for a D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).1-approximation (Li et al., 2021). A common misconception is that Hamming-distance techniques suffice whenever the alphabet is discrete; these lower bounds and algorithmic separations show that insertions and deletions fundamentally change the space complexity (Li et al., 2021).

5. Embeddings and learned representations

A central approximation strategy is to embed strings into a simpler metric space while preserving ED structure. CNN-ED learns a map

D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).2

such that

D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).3

with a loss combining triplet loss for order preservation and pairwise approximation loss for metric fidelity (Dai et al., 2020). Its input representation is a one-hot matrix over the alphabet and sequence positions, the network uses several 1D convolution layers and pooling, and the default embedding dimension is D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).4 (Dai et al., 2020).

The theoretical analysis isolates ED-preserving components of the architecture. One-hot encoding decomposes a string into per-character binary sequences whose summed binary edit distances satisfy explicit upper and lower bounds relative to the original ED, and max-pooling on binary sequences preserves ED up to controlled additive and multiplicative terms (Dai et al., 2020). These results explain why convolution-plus-pooling can be well aligned with insertions, deletions, and local shifts. Empirically, CNN-ED outperforms data-independent CGK embedding and RNN-based GRU embedding in both accuracy and efficiency, and string similarity search can be accelerated, sometimes by orders of magnitude (Dai et al., 2020).

This line sits between classical metric embedding and ANN-oriented systems design. Data-independent CGK maps ED into Hamming distance with distortion polynomial in the true ED, while CNN-ED uses data-dependent learning to produce compact fixed-dimensional vectors more suitable for Euclidean ANN indexes (Dai et al., 2020). A plausible implication is that modern vector-search infrastructure can be repurposed for approximate ED workloads when the embedding is sufficiently faithful.

6. Variants, generalizations, and application domains

Weighted ED interpolates between different similarity models. In D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).5, insertions and deletions cost D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).6 and substitutions cost D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).7, so D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).8 is standard Levenshtein distance and large D[i,j]=min(D[i1,j]+1,  D[i,j1]+1,  D[i1,j1]+1[sx[i]sy[j]]).D[i,j] = \min\Big( D[i-1,j]+1,\; D[i,j-1]+1,\; D[i-1,j-1]+\mathbb{1}[s_x[i]\neq s_y[j]] \Big).9 yields a Hamming-like regime (Goldenberg et al., 2022). The same work studies I(a,i)I(a,i)0-alignments, which separately bound the number of indels and substitutions, and gives an exact algorithm for the decision problem in

I(a,i)I(a,i)1

time as well as a bicriteria approximation algorithm in

I(a,i)I(a,i)2

time (Goldenberg et al., 2022). This makes precise the idea that ED can be tuned to domains where substitutions and indels have very different frequencies or semantics.

At the language level, ED becomes a quantitative inclusion measure for automata. For pushdown automata as sources and finite automata as targets, threshold edit distance is EXPTIME-complete, finite edit distance to NFA is EXPTIME-complete, finite edit distance to DFA is coNP-complete, and edit distance to a pushdown automaton is undecidable (Chatterjee et al., 2015). The finite-distance side also has a structural dichotomy: for PDA/DPDA to NFA, the distance is either exponentially bounded or infinite (Chatterjee et al., 2015).

Geometric ED extends the same dynamic-programming logic to point sequences in I(a,i)I(a,i)3. For I(a,i)I(a,i)4-packed and I(a,i)I(a,i)5-bounded curves, and for backbone sequences, there are the first subquadratic-time approximation schemes for dynamic time warping and edit distance of point sequences, with I(a,i)I(a,i)6-approximation time near-linear for I(a,i)I(a,i)7-packed and I(a,i)I(a,i)8-bounded curves and subquadratic for backbone sequences (Agarwal et al., 2015). The algorithmic mechanism is to cover the dynamic-programming table by a small set of rectangles whose entry weights are approximately constant (Agarwal et al., 2015).

Application domains reflect these variants. Standard ED is directly tied to spell correction, data de-duplication, and sequence alignment (Dai et al., 2020). In robust NLP, CERT-ED adapts randomized deletion to certify robustness of text classifiers against token-level edit distance perturbations, covering insertions, deletions, and substitutions; it outperforms the Hamming-distance method RanMASK in 4 out of 5 datasets in terms of both accuracy and the cardinality of the certificate, and improves empirical robustness in 38 out of 50 attack settings (Huang et al., 2024). This underscores that ED is not only an algorithmic primitive but also an operational threat model for discrete, variable-length data.

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Edit Distance (ED).