Opening your learning path
Progress is saved as you study.
Opening your learning path
Progress is saved as you study.
The 2D Array (Edit Distance / Levenshtein Distance) pattern applies when you need the 2D Array (Edit Distance / Levenshtein Distance) technique within the Dynamic Programming (DP) Patterns pattern. Its time complexity is O(m × n) and space complexity O(m × n). It is used in 4 problems on Thita, including Delete Operation for Two Strings, Edit Distance and Minimum ASCII Delete Sum for Two Strings. Common variations are 1D Array (Word Break Style) and Longest Increasing Subsequence (LIS).
Calculate minimum edit operations to transform one string to another using 2D DP.
2D Array (Edit Distance / Levenshtein Distance) is one of the 12 subpatterns of the Dynamic Programming (DP) Patterns pattern, which covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break. The whole pattern is about 5 hours of study. This subpattern is an important variation worth recognising on sight.
Master DP patterns including Fibonacci, Kadane, knapsack, LCS, LIS, edit distance, and grid paths. Solve optimization problems with overlapping subproblems. Problems in this subpattern are usually searched for as edit distance, Levenshtein distance, string transformation, 2D DP, leetcode 72.
Read the theory for 2D Array (Edit Distance / Levenshtein Distance), then work the problems attached to it in the browser editor. Your solution runs against the problem's test cases, and the AI coach offers a hint about the technique you are missing rather than a finished solution. Progress is tracked per subpattern, so the Dynamic Programming (DP) Patterns tracker shows this one as covered once you have solved its problems.
2D Array (Edit Distance / Levenshtein Distance) is one lesson in a 16-pattern DSA course. If you are preparing end to end, work the interview-critical patterns first and use the pattern sheet as the checklist; if you are here for one technique, the Dynamic Programming (DP) Patterns guide is the shortest path back to the rest of it.
4 problems, easiest first.
Imagine you’re trying to turn the word “cat” into “cut.” You can either change a letter, add a new one, or remove one. How many steps does it take to make the transformation? This is the heart of the Edit Distance problem—a classic example of the 2D Array Dynamic Programming pattern.
Think of it like editing a document: you might need to insert, delete, or replace words to get from your first draft to the final version. Each change is a small step, and you want to make as few edits as possible. This pattern helps us find the minimum number of operations needed to convert one string into another.
This approach is especially useful in scenarios like:
Whenever you need to compare two sequences (like strings or arrays) and measure how “close” they are, this pattern shines. It’s like finding the shortest path to transform one thing into another, step by step, using a set of allowed moves.
Let’s break down the magic behind the Edit Distance problem using dynamic programming (DP):
Imagine a grid (2D table) where rows represent the first string (word1) and columns represent the second string (word2). Each cell (i, j) in this table tells us: What’s the minimum number of edits needed to convert the first i letters of word1 into the first j letters of word2?
word1 (move right in the table).word1 (move down).word1 to match word2 (move diagonally).At each cell, you pick the option with the smallest cost and add one (for the operation you just did).
This approach is based on recurrence relations—breaking a big problem into smaller, overlapping subproblems and building up the solution. The DP table stores answers to these subproblems so we don’t repeat work.
Let’s walk through how you’d approach an Edit Distance problem:
dp of size (m+1) x (n+1), where m and n are the lengths of the two strings.dp[i][j] will represent the minimum edits needed to convert the first i characters of word1 to the first j characters of word2.dp[0][j] = j (insert all j characters)dp[i][0] = i (delete all i characters)For each cell (i, j):
word1[i-1] == word2[j-1]: No operation needed, so dp[i][j] = dp[i-1][j-1].dp[i][j-1] + 1dp[i-1][j] + 1dp[i-1][j-1] + 1dp[m][n], representing the minimum edits to convert the entire word1 to word2.Let’s implement the Edit Distance problem step by step:
1#include <iostream>
2#include <vector>
3#include <string>
4using namespace std;
5
6// Function to compute the minimum edit distance between two words
7int minEditDistance(const string& word1, const string& word2) {
8 int m = word1.size();
9 int n = word2.size();
10
11 // Create a DP table of size (m+1) x (n+1)
12 vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
13
14 // Initialize base cases:
15 // If word1 is empty, we need to insert all characters of word2
16 for (int j = 0; j <= n; ++j) {
17 dp[0][j] = j;
18 }
19 // If word2 is empty, we need to delete all characters of word1
20 for (int i = 0; i <= m; ++i) {
21 dp[i][0] = i;
22 }
23
24 // Fill the DP table
25 for (int i = 1; i <= m; ++i) {
26 for (int j = 1; j <= n; ++j) {
27 if (word1[i - 1] == word2[j - 1]) {
28 // Characters match, no operation needed
29 dp[i][j] = dp[i - 1][j - 1];
30 } else {
31 // Consider insert, delete, and replace operations
32 int insertOp = dp[i][j - 1] + 1; // Insert word2[j-1] into word1
33 int deleteOp = dp[i - 1][j] + 1; // Delete word1[i-1]
34 int replaceOp = dp[i - 1][j - 1] + 1; // Replace word1[i-1] with word2[j-1]
35 dp[i][j] = min({insertOp, deleteOp, replaceOp});
36 }
37 }
38 }
39
40 // The answer is in the bottom-right cell
41 return dp[m][n];
42}
43
44int main() {
45 string word1 = "intention";
46 string word2 = "execution";
47 cout << "Minimum Edit Distance: " << minEditDistance(word1, word2) << endl;
48 // Output: Minimum Edit Distance: 5
49 return 0;
50}Code Walkthrough:
dp to store solutions to subproblems.dp[m][n].Let’s analyze the Edit Distance problem step by step, as if explaining to a friend:
Step 1: Understand the Problem
Edit distance horse to ros: each cell is the fewest edits from the first i letters of horse to the first j letters of ros. Matches copy the diagonal; otherwise 1 + the smallest of left, above and diagonal. The corner cell holds 3.
We’re given two words, say "horse" and "ros", and we want to find the minimum number of edits (insert, delete, replace) needed to turn one into the other.
Step 2: Break It Down
word1 to the first part of word2?”Step 3: Build the DP Table
"ho" to "r", we might delete 'h' and replace 'o' with 'r'.Step 4: Fill the Table
Step 5: Connect Back to the Pattern
This is classic 2D DP: we’re using a table to avoid recalculating the same subproblems. Each cell depends on its neighbors (left, above, diagonal), just like moving through a maze with three possible moves at each step.
Step 6: Read the Answer
The answer is in the bottom-right cell of the table: the minimum edits needed for the whole words.
Signals to Recognize This Pattern:
Aha! Moments:
Debugging Tips:
Memory Tricks:
With practice, you’ll spot Edit Distance problems quickly and know exactly how to tackle them!