72. Edit Distance
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of single-character edits (insertions, deletions, or replacements) required to transform one string, word1, into another, word2. This is a classic dynamic programming problem, commonly known as computing the Levenshtein distance between two strings.
Solution
Edit Distance in Python
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m, n = len(word1), len(word2)
# dp[i][j] will be the minimum distance to convert
# the first i characters of word1 to the first j characters of word2.
dp = [[0] * (n + 1) for _ in range(m + 1)]
# Base cases
# To convert a string of length i to an empty string, we need i deletions.
for i in range(m + 1):
dp[i][0] = i
# To convert an empty string to a string of length j, we need j insertions.
for j in range(n + 1):
dp[0][j] = j
# Fill the rest of the dp table
for i in range(1, m + 1):
for j in range(1, n + 1):
# If the last characters are the same, no new operation is needed.
# The cost is the same as converting the strings without these characters.
if word1[i - 1] == word2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
# If last characters are different, we consider three operations:
# 1. Replace: cost is 1 + dp[i-1][j-1]
# 2. Delete: cost is 1 + dp[i-1][j]
# 3. Insert: cost is 1 + dp[i][j-1]
# We take the minimum of these three possibilities.
dp[i][j] = 1 + min(dp[i - 1][j - 1], # Replace
dp[i - 1][j], # Delete
dp[i][j - 1]) # Insert
return dp[m][n]
Edit Distance in Java
class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
// dp[i][j] will be the minimum distance to convert
// the first i characters of word1 to the first j characters of word2.
int[][] dp = new int[m + 1][n + 1];
// Base cases
// To convert a string of length i to an empty string, we need i deletions.
for (int i = 0; i <= m; i++) {
dp[i][0] = i;
}
// To convert an empty string to a string of length j, we need j insertions.
for (int j = 0; j <= n; j++) {
dp[0][j] = j;
}
// Fill the rest of the dp table
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
// If the last characters are the same, no new operation is needed.
// The cost is the same as converting the strings without these characters.
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
// If last characters are different, we consider three operations:
// 1. Replace: cost is 1 + dp[i-1][j-1]
// 2. Delete: cost is 1 + dp[i-1][j]
// 3. Insert: cost is 1 + dp[i][j-1]
// We take the minimum of these three possibilities.
int replace = dp[i - 1][j - 1];
int delete = dp[i - 1][j];
int insert = dp[i][j - 1];
dp[i][j] = 1 + Math.min(replace, Math.min(delete, insert));
}
}
}
return dp[m][n];
}
}
Edit Distance in C++
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length();
int n = word2.length();
// dp[i][j] will be the minimum distance to convert
// the first i characters of word1 to the first j characters of word2.
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
// Base cases
// To convert a string of length i to an empty string, we need i deletions.
for (int i = 0; i <= m; ++i) {
dp[i][0] = i;
}
// To convert an empty string to a string of length j, we need j insertions.
for (int j = 0; j <= n; ++j) {
dp[0][j] = j;
}
// Fill the rest of the dp table
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
// If the last characters are the same, no new operation is needed.
// The cost is the same as converting the strings without these characters.
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
// If last characters are different, we consider three operations:
// 1. Replace: cost is 1 + dp[i-1][j-1]
// 2. Delete: cost is 1 + dp[i-1][j]
// 3. Insert: cost is 1 + dp[i][j-1]
// We take the minimum of these three possibilities.
dp[i][j] = 1 + min({dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]});
}
}
}
return dp[m][n];
}
};
Edit Distance in TypeScript
function minDistance(word1: string, word2: string): number {
const m = word1.length;
const n = word2.length;
// dp[i][j] will be the minimum distance to convert
// the first i characters of word1 to the first j characters of word2.
const dp: number[][] = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0));
// Base cases
// To convert a string of length i to an empty string, we need i deletions.
for (let i = 0; i <= m; i++) {
dp[i][0] = i;
}
// To convert an empty string to a string of length j, we need j insertions.
for (let j = 0; j <= n; j++) {
dp[0][j] = j;
}
// Fill the rest of the dp table
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
// If the last characters are the same, no new operation is needed.
// The cost is the same as converting the strings without these characters.
if (word1[i - 1] === word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
// If last characters are different, we consider three operations:
// 1. Replace: cost is 1 + dp[i-1][j-1]
// 2. Delete: cost is 1 + dp[i-1][j]
// 3. Insert: cost is 1 + dp[i][j-1]
// We take the minimum of these three possibilities.
dp[i][j] = 1 + Math.min(
dp[i - 1][j - 1], // Replace
dp[i - 1][j], // Delete
dp[i][j - 1] // Insert
);
}
}
}
return dp[m][n];
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.