Edit Distance
A cell reads the row above it and the cell to its left. So keep two rows: prev (row i − 1) and cur (row i). Fill cur from left to right, then swap the two. The last value of prev is the answer.
A cell is correct when its last letters are handled: a match copies the diagonal for free, and a mismatch pays 1 for the cheapest of insert, delete or replace.
Problem
You have two words, word1 and word2. Find the fewest operations that turn word1 into word2.
You may do three things to a word: insert one letter, delete one letter, or replace one letter with another. Each operation counts as 1, whatever letters are involved.
Two strings word1 and word2. Either one can be empty.
A single number: the fewest operations that turn word1 into word2.
Replace h with r (rorse), delete r (rose), delete e (ros). No route uses only two operations.
Delete t, replace i with e, replace n with x, replace n with c, and insert u. Five operations.
- • 0 ≤ word1.length, word2.length ≤ 500
- • word1 and word2 consist of lowercase English letters.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If the last letters of the two words are the same, does that last letter need an operation at all?
- 2.
If the last letters differ, which of the three operations can fix them, and what does each one leave behind?
- 3.
After one operation, which smaller pair of words is left to solve? Is it shorter in word1, in word2, or both?
- 4.
How many operations turn an empty word into a word of 3 letters? How many turn a word of 3 letters into an empty word?
- 5.
Two words of length 5 and 3 share no letter at all. What is the fewest number of operations, without working it out letter by letter?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Recurse on (i, j): a match is free, otherwise take the cheapest of three moves | O(3^(N + M)) | O(N + M) |
| Better | A table with one cell per pair of prefixes; each cell is filled once from three cells before it | O(N · M) | O(N · M) |
| Optimal | A cell reads only the row above and the cell to its left, so two rows are enough | O(N · M) | O(M) |
Why does this belong to this pattern?
- “fewest operations”A minimum over several choices. The table keeps the cheapest value for each pair of prefixes.
- “insert, delete or replace”Three moves on the last letter, so each cell reads three cells. This is what makes it edit distance.
- “turn word1 into word2”Two strings, so the table has one axis for each string.
This is the anchor of the sub-pattern. Each cell takes the cheapest of three moves on the last letters, and a match costs nothing. The next questions remove moves: Minimum ASCII Delete Sum keeps only the delete moves and weights each letter, and Interleaving String only asks whether each cell can be true. LCS looks similar, but it has no replace move, so it is not this table.
Don't make this mistake
LCS counts only insert and delete. A replace is one delete plus one insert there, so it costs 2, but it costs 1 here. On horse and ros the longest common subsequence has 2 letters (for example os), so n + m − 2 × LCS = 5 + 3 − 4 = 4. The real answer is 3: replace h with r, delete r, delete e.
Turning an empty word1 into j letters takes j inserts, not 0. With the base at 0, horse → ros gives 2 instead of 3, and an empty word1 → ab gives 0 instead of 2.
Matching letters are kept for free. Charging 1 for them makes a → a cost 1 instead of 0.
When we reach column j, dp[j − 1] has already been overwritten with the new row. It is the cell to the left, not the cell above-left, so the diagonal is lost. On horse → ros this gives 6 instead of 3.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Strings translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.