By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This single problem tests recursion, dynamic programming, and space optimization—master it, and you’ll crush 1 in 4 DP questions in FAANG onsites."
Before tackling Edit Distance, ensure you understand: 1. Recursion & Memoization – Breaking problems into smaller subproblems. 2. Dynamic Programming (DP) Basics – Overlapping subproblems, optimal substructure. 3. 2D DP Tables – Storing intermediate results in a grid.
memo[i][j] = min(insert, delete, replace)
dp[i][j]
word1[0..i]
word2[0..j]
i
j
dp[0][j] = j
dp[i][0] = i
if word1[i-1] == word2[j-1]: dp[i][j] = dp[i-1][j-1]
else: dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])
Follow these steps every time you see an Edit Distance problem:
Ask: "Are substitutions allowed? Is cost uniform (1 per op)?"
Define Subproblem
Let dp[i][j] = min operations to convert word1[0..i-1] to word2[0..j-1].
word1[0..i-1]
word2[0..j-1]
Base Cases
word1
i=0
word2
If word2 is empty (j=0), delete all i chars of word1 → dp[i][0] = i.
j=0
Recurrence Relation
word1[i-1] == word2[j-1]
dp[i][j] = dp[i-1][j-1]
Else → Take min of 3 operations + 1:
dp[i][j-1]
dp[i-1][j]
dp[i-1][j-1]
Choose Approach
Bottom-Up DP (Tabulation) → Preferred for interviews (avoids stack overflow).
Optimize Space (Follow-up)
Use two 1D arrays (prev and curr) instead of full 2D table.
prev
curr
Edge Cases
One string is a subsequence of the other.
Code & Test
dp[m][n]
Problem: Given two strings word1 and word2, return the minimum number of operations to convert word1 to word2.
Solution (Bottom-Up DP):
def minDistance(word1: str, word2: str) -> int: m, n = len(word1), len(word2) dp = [[0] (n + 1) for _ in range(m + 1)] # Base cases for i in range(m + 1): dp[i][0] = i for j in range(n + 1): dp[0][j] = j for i in range(1, m + 1): for j in range(1, n + 1): if word1[i-1] == word2[j-1]: dp[i][j] = dp[i-1][j-1] else: dp[i][j] = 1 + min(dp[i-1][j], # Delete dp[i][j-1], # Insert dp[i-1][j-1]) # Replace return dp[m][n]
Time Complexity: O(mn) (filling a 2D table). Space Complexity: O(mn) (can be optimized to O(n)).
Why This Works: - The DP table tracks the min operations for all prefixes of word1 and word2. - If characters match, we carry forward the previous result (dp[i-1][j-1]). - If they differ, we take the cheapest of the 3 possible operations.
Problem: Solve the same problem, but optimize space to O(n).
Solution (1D DP):
def minDistance(word1: str, word2: str) -> int: m, n = len(word1), len(word2) prev = [j for j in range(n + 1)] for i in range(1, m + 1): curr = [0] (n + 1) curr[0] = i for j in range(1, n + 1): if word1[i-1] == word2[j-1]: curr[j] = prev[j-1] else: curr[j] = 1 + min(prev[j], # Delete curr[j-1], # Insert prev[j-1]) # Replace prev = curr return prev[n]
Time Complexity: O(mn). Space Complexity: O(n) (only two 1D arrays).
Why This Works: - We only need the previous row (prev) to compute the current row (curr). - This reduces space from O(mn) → O(n) without changing the logic.
Problem: Suppose insert/delete costs 1, but replace costs 2. Modify the solution.
Solution:
def minDistance(word1: str, word2: str) -> int: m, n = len(word1), len(word2) dp = [[0] (n + 1) for _ in range(m + 1)] for i in range(m + 1): dp[i][0] = i for j in range(n + 1): dp[0][j] = j for i in range(1, m + 1): for j in range(1, n + 1): if word1[i-1] == word2[j-1]: dp[i][j] = dp[i-1][j-1] else: dp[i][j] = min( dp[i-1][j] + 1, # Delete dp[i][j-1] + 1, # Insert dp[i-1][j-1] + 2 # Replace (cost = 2) ) return dp[m][n]
Time Complexity: O(mn). Space Complexity: O(mn).
Why This Works: - The only change is adjusting the replace cost from 1 to 2. - The DP transition remains the same, but weights are updated.
1
2
word1[i-1]
word2[j-1]
dp[0][0] = 0
min
dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1])
"Alright, let’s lock this in. Edit Distance is a classic DP problem where you convert one string to another using insert, delete, or replace. Here’s the playbook:
This pattern shows up in spell checkers, DNA sequencing, and even plagiarism detection. Nail it, and you’ll handle any DP string problem thrown at you. Now go crush that interview!
This guide is 100% interview-ready—every line is actionable under pressure. Good luck! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.