induwara.lk
induwara.lkStrings · Algorithms

Levenshtein Distance Calculator

Find the edit distance between two strings — the fewest single-character insertions, deletions, and substitutions to turn one into the other. See the full dynamic-programming matrix and the step-by-step edit script. Runs entirely in your browser, no signup.

By Induwara AshinsanaUpdated Jun 10, 2026
Edit distance
Cross-checked ✓
6 / 2,000
7 / 2,000
Examples
Edit distance
3
Similarity
57.1%
1 − distance / max(len)
Matched chars
4
String lengths
6 · 7
len A · len B (after toggles)

Operation breakdown

Substitutions
2
Insertions
1
Deletions
0
Matches
4

Aligned edit script

sub'k' → 's'keep'i'keep't'keep't'sub'e' → 'i'keep'n'ins'g'

Dynamic-programming matrix

εsitting
ε01234567
k11234567
i22123456
t33212345
t44321234
e55432234
n66543323

Sources cited: Wagner & Fischer (1974), The String-to-String Correction Problem; Levenshtein (1966); NIST DADS. Unit cost per insertion, deletion, and substitution. The full list with links is in the references section below.

How it works

The calculator uses the Wagner–Fischer dynamic-programming algorithm, the standard method for computing the Levenshtein (edit) distance defined by Vladimir Levenshtein in 1966. Given two strings A of length m and B of length n, it fills a table of size (m+1) × (n+1) where each cell d[i][j] holds the edit distance between the first i characters of A and the first j characters of B.

  1. Apply the pre-processing toggles first: lowercase both strings if the comparison is case-insensitive, and strip whitespace if that option is on.
  2. Initialise the edges: d[i][0] = i (turning a prefix into an empty string costs i deletions) and d[0][j] = j (building from empty costs j insertions).
  3. Fill each remaining cell with the recurrence below, where the substitution cost is 0 when the characters match and 1 when they differ:
    cost = (A[i-1] == B[j-1]) ? 0 : 1
    d[i][j] = min(
      d[i-1][j]   + 1,   // deletion
      d[i][j-1]   + 1,   // insertion
      d[i-1][j-1] + cost // substitution / match
    )
  4. The Levenshtein distance is the bottom-right cell, d[m][n].
  5. Backtrack from d[m][n] to d[0][0], at each step choosing the predecessor that produced the minimum, to recover one optimal edit script and the per-operation counts. Ties are broken in a fixed, documented order: match, substitution, deletion, insertion.

The algorithm runs in O(m × n) time. The similarity percentage shown alongside the distance is a presentation convenience, computed as (1 − distance / max(m, n)) × 100 and defined as 100% when both strings are empty; it is not part of the formal Levenshtein definition. Every result is independently cross-checked against a second, space-optimized two-row implementation of the same recurrence — if the two ever disagreed, the badge in the tool would flag it.

Worked examples

kitten → sitting (the textbook case)

distance = 3 · similarity ≈ 57.1%

  1. Align the strings: k i t t e n vs s i t t i n g
  2. Substitute k → s (position 1) ............ cost 1
  3. Keep i, t, t (already matching) .......... cost 0
  4. Substitute e → i (position 5) ............ cost 1
  5. Keep n .................................... cost 0
  6. Insert g at the end ...................... cost 1
  7. Total = 3 edits. max(len) = 7, so similarity = (1 − 3/7) × 100 ≈ 57.1%

Saturday → Sunday

distance = 3 · similarity = 62.5%

  1. Keep S .................................... cost 0
  2. Delete a, delete t (from "Sat…") ......... cost 2
  3. Keep u .................................... cost 0
  4. Substitute r → n .......................... cost 1
  5. Keep d, a, y .............................. cost 0
  6. Total = 3 edits. max(len) = 8, so similarity = (1 − 3/8) × 100 = 62.5%

flaw → lawn (edge: empty-ish overlap)

distance = 2 · similarity = 50.0%

  1. The substring "law" is shared by both strings
  2. Delete f from the front of "flaw" ........ cost 1
  3. Keep l, a, w .............................. cost 0
  4. Insert n at the end to reach "lawn" ...... cost 1
  5. Total = 2 edits. max(len) = 4, so similarity = (1 − 2/4) × 100 = 50.0%

Frequently asked questions

Sources & references

The algorithm, recurrence, and worked examples on this page were last cross-checked against these sources on 2026-06-10. The distance is deterministic and verified against an independent second implementation on every calculation.

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.