ML Models & Algorithms
Levenshtein Distance
In shortThe minimum number of single-character insertions, deletions or substitutions needed to turn one string into another.
Levenshtein distance counts the smallest number of single-character edits (insertions, deletions or substitutions) needed to change one string into another. The lower the number, the more similar the strings. It is the classic measure behind distance-based fuzzy matching and the workhorse of many SEO clean-up scripts.
How it works
The textbook example is “kitten” to “sitting”, which takes three edits: substitute k with s, substitute e with i, and insert g at the end. Under the hood, the algorithm fills in a grid comparing every prefix of one string with every prefix of the other, which guarantees the minimum is found.
Raw distances are hard to compare across strings of different lengths, so most tools convert them into a similarity score, often 0 to 100. Be careful here: the popular ratio function in RapidFuzz and FuzzyWuzzy is based on insertions and deletions only (the Indel distance), so it can rank pairs differently from pure Levenshtein. RapidFuzz includes a dedicated Levenshtein module when you need the exact measure.
How it’s used in SEO and marketing
- 404 mapping. Match broken URLs to the closest live URL.
- Site migrations. Map old URLs to new ones in bulk; MLforSEO’s triple fuzzy matching notebook compares URLs, titles and headings together.
- Keyword mapping. Assign keyword variants and misspellings to seed terms or topics.
- Metadata comparison. Find titles that are almost identical to competitors’ or to each other.
Example
A crawl finds a 404 at /blog/keyword-reserch-guide. Comparing it with live URLs, /blog/keyword-research-guide is one insertion away (the missing “a”), so it scores far higher than any other candidate and becomes the redirect target. A quick human check confirms the match before the redirect goes live.
Limitations and things to watch
- It has no idea of meaning. HARD and HAND are one edit apart, while HARD and HARDER are two, even though a person sees the second pair as more related.
- Swapped letters count as two edits; the Damerau-Levenshtein variant counts them as one.
- The cost of each comparison grows with the length of both strings, and comparing every pair in large lists adds up quickly, so narrow the candidates first.
- Pick and validate thresholds on a sample you have checked by hand.
Learn it on MLforSEO
Where Levenshtein Distance shows up on MLforSEO
Learn it in a course
Read about it on the blog
Try it yourself
- Coding Script or NotebookCalculating Semantically Similar Terms with Fuzzy Matching for Keyword Clusers (Notebook)
- Coding Script or NotebookMapping 404 URLs to Live Pages with Fuzzy Matching (Guide)
- GuideComprehensive String Matching & Fuzzy Matching Reference Guide for SEO Applications: Use Cases, Approaches, Models
Sources and further reading
Other terms from ML Models & Algorithms
- BERT (Bidirectional Encoder Representations from Transformers)Google's 2018 transformer language model that reads text in both directions to produce context-aware embedding…
- BERTopicA modular topic modelling technique that clusters transformer embeddings and labels each cluster with class-ba…
- BIRCH (Balanced Iterative Reducing and Clustering using Hierarchies)BIRCH (Balanced Iterative Reducing and Clustering using Hierarchies) clusters very large numeric datasets by s…
- BM25A classic ranking function that scores documents by how often query words appear, adjusted for word rarity and…
- Boyer-MooreA fast exact string-search algorithm that compares a pattern from right to left and skips ahead after mismatch…
- c-TF-IDFClass-based TF-IDF: treats each cluster as one document to find the words that best describe it, used by BERTo…
- Centroid-based ClusteringA clustering family that groups data around central points (centroids), each the average of its cluster's memb…
- Convolutional Neural Network (CNN)A deep learning architecture that scans data with small learned filters to detect local patterns; the classic…
- DBSCANDensity-based clustering that groups closely packed points, finds the number of clusters itself and labels iso…

