Python – Edit Distance
The Edit Distance problem, also known as the Levenshtein Distance, measures the minimum number of operations required to convert one string into another. The allowed operations are insertion, deletion, or substitution of a single character.
This tutorial will walk you through a dynamic programming approach to solve the Edit Distance problem in Python, with detailed explanations for each example.
Problem Statement
Given two strings word1 and word2, determine the minimum number of operations required to convert word1 into word2. The permitted operations are:
- Insert a character
- Delete a character
- Replace a character
Sample Input and Output
Example 1:
# Input:
word1 = "kitten"
word2 = "sitting"
# Output:
3
Explanation: To convert “kitten” into “sitting”, the following operations are performed: 1. Replace ‘k’ with ‘s’ → “sitten” 2. Replace ‘e’ with ‘i’ → “sittin” 3. Insert ‘g’ at the end → “sitting” Thus, the minimum number of operations required is 3.
Example 2:
# Input:
word1 = "horse"
word2 = "ros"
# Output:
3
Explanation: To convert “horse” into “ros”, the following operations are performed: 1. Delete ‘r’ → “hose” 2. Delete ‘e’ → “hos” 3. Replace ‘h’ with ‘r’ → “ros” Thus, the minimum number of operations required is 3.
Solution Approach
We solve the problem using dynamic programming by constructing a 2D table dp where dp[i][j] represents the minimum edit distance between the first i characters of word1 and the first j characters of word2.
The steps to build the DP table are as follows:
- Initialize a table
dpof size(m+1) x (n+1), wheremandnare the lengths ofword1andword2respectively. - Set
dp[i][0] = ifor alli(convertingword1to an empty string requiresideletions) anddp[0][j] = jfor allj(converting an empty string toword2requiresjinsertions).
- For each character in
word1(indexi) andword2(indexj):- If
word1[i-1] == word2[j-1], then no new operation is needed, sodp[i][j] = dp[i-1][j-1]. - If they differ, choose the minimum cost among:
dp[i-1][j](deletion)dp[i][j-1](insertion)dp[i-1][j-1](replacement)
- If
