Edit Distance in Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Edit Distance is a fundamental concept in computer science when it comes to measuring how different two strings are from each other. It has wide applications in areas like natural language processing, bioinformatics, and spell correction. In this article, we discuss the concept of edit distance, particularly focusing on the Levenshtein distance, and demonstrate how to compute it using Python.
What is Edit Distance?
Edit distance is a way of quantifying how dissimilar two strings (e.g., words) are. The edit distance between two strings is the minimum number of operations required to transform one string into the other. The operations typically allowed are:
- Insertion: Adding a character.
- Deletion: Removing a character.
- Substitution: Replacing one character with another.
For example, the edit distance between the strings "kitten" and "sitting" is 3, due to the following transformations:
- kitten → sitten (substitution of 'k' with 's')
- sitten → sittin (substitution of 'e' with 'i')
- sittin → sitting (insertion of 'g')
Levenshtein Distance
The Levenshtein distance is perhaps the most commonly used edit distance, where each operation (insertion, deletion, substitution) has a cost of 1. It can be effectively computed using dynamic programming.
Dynamic Programming Approach
The dynamic programming approach to calculate the Levenshtein distance involves filling up a 2D table (matrix) to store the results of subproblems. The table helps in reducing the time complexity from an exponential one (like with recursion) to a polynomial one.
Python Implementation
- Time Complexity: The time complexity of calculating the Levenshtein distance using dynamic programming is , where
mandnare the lengths of the strings. - Space Complexity: It requires space for the DP table. However, this can be optimized to using a single row/column array.
- Limitations: The method is mainly suited for short string comparisons due to quadratic space requirements. Other specialized algorithms exist for longer texts and specific use cases.
Related reading
- Edit distance recursive algorithm -- Skiena
- Edmonds-Karp Algorithm for a graph which has nodes with flow capacities
- Effect of randomness on search results
- Effective queries in machine learning
- Edit tensorflow inceptionV3 retraining-example.py for multiple classificiations
- Efficent way to split a large text file in python
- Effective unique on unordered elements
- Effectively sorting when your data is distributed across different microservices

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.