Levenshtein Distance Calculator
Levenshtein distance counts the cheapest sequence of insertions, deletions and substitutions that turns one string into another — the metric behind spell checkers, deduplication, fuzzy joins and 'did you mean' search. This calculator gives you the distance, the full dynamic-programming matrix so you can see how the answer was reached, the exact edit path, and the numbers you would get from Damerau-Levenshtein, Hamming distance and PostgreSQL's pg_trgm similarity(). It also writes the SQL for doing the same thing in a database with fuzzystrmatch and a trigram index. Everything is computed in your browser — no text is sent anywhere.
Do more than levenshtein distance calculator — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Type or paste the two strings you want to compare.
- Optionally change the insert, delete and substitute costs, or switch to case-insensitive comparison for a real-world match.
- Read the distance and similarity, inspect the matrix and edit path to understand the result, and copy the SQL to run the same comparison in PostgreSQL.
Frequently asked questions
What is Levenshtein distance and how is it calculated?
It is the minimum number of single-character edits — insertions, deletions and substitutions — needed to change one string into another. The standard algorithm (Wagner-Fischer) fills an (m+1) × (n+1) matrix where cell [i][j] is the distance between the first i characters of one string and the first j of the other. Each cell is the smallest of three options: the cell above plus a deletion, the cell to the left plus an insertion, and the diagonal cell plus a substitution (or plus zero if the characters match). The bottom-right cell is the distance. 'kitten' to 'sitting' is 3: substitute k→s, substitute e→i, and insert g.
What is the difference between Levenshtein and Damerau-Levenshtein distance?
Damerau-Levenshtein adds a fourth operation: transposing two adjacent characters counts as one edit. Plain Levenshtein scores 'form' against 'from' as 2, because it must substitute twice; Damerau-Levenshtein scores it as 1. For human typing errors, transposition is one of the most common mistakes, so Damerau usually ranks candidates better. The variant most implementations use, including the one here, is optimal string alignment, which does not allow a substring to be edited more than once.
How do I use Levenshtein distance in PostgreSQL?
Install the fuzzystrmatch extension and call levenshtein(a, b), optionally with custom insert, delete and substitute costs, or levenshtein_less_equal(a, b, max) to abort early past a threshold. The catch is that no index can accelerate it, so a query filtering on levenshtein() scans the whole table. The practical pattern is two-stage: create a GIN index with gin_trgm_ops from pg_trgm, filter candidates with the % operator or similarity(), then re-rank that small set with levenshtein(). To run these queries and manage extensions across PostgreSQL, MySQL and 20+ other databases, use Chat2DB — download it at https://chat2db.ai/download or open https://app.chat2db.ai.
