Skip to content

Commit cbeaec2

Browse files
authored
Merge pull request #95 from pertrai1/leetcode-12222025
LeetCode Edit Distance Problem
2 parents 1a28970 + 95ecd4e commit cbeaec2

File tree

5 files changed

+110
-51
lines changed

5 files changed

+110
-51
lines changed

0072-edit-distance/README.md

Lines changed: 0 additions & 47 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ A comprehensive collection of coding challenges from multiple platforms for lear
1818
**Progress**:
1919

2020
- Easy: 80 problems
21-
- Medium: 71 problems
21+
- Medium: 72 problems
2222
- Hard: 13 problems
2323

2424
**Quick Links**:
@@ -234,6 +234,7 @@ A comprehensive collection of coding challenges from multiple platforms for lear
234234
### Dynamic Programming
235235

236236
- [0053 - Maximum Subarray](./leetcode/medium/0053-maximum-subarray) ![Medium](https://img.shields.io/badge/Medium-orange)
237+
- [0072 - Edit Distance](./leetcode/medium/0072-edit-distance) ![Medium](https://img.shields.io/badge/Medium-orange)
237238
- [0139 - Word Break](./leetcode/medium/0139-word-break) ![Medium](https://img.shields.io/badge/Medium-orange)
238239
- [0198 - House Robber](./leetcode/medium/0198-house-robber) ![Medium](https://img.shields.io/badge/Medium-orange)
239240
- [0413 - Arithmetic Slices](./leetcode/medium/0413-arithmetic-slices) ![Medium](https://img.shields.io/badge/Medium-orange)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Post-Mortem Log
2+
3+
## Problem
4+
5+
Given two inputs, `word1: string`, `word2: string`, return the total number of operations needed to have `word1` match `word2`. The operations available are `insert`, `replace`, `delete`.
6+
7+
- **Problem Name:** Edit Distance
8+
- **Problem Link (if applicable):** [https://leetcode.com/problems/edit-distance](https://leetcode.com/problems/edit-distance)
9+
- **Date:** 12/22/2025
10+
11+
---
12+
13+
## Time Tracking
14+
15+
- **Time to design the algorithm:** 25 minutes
16+
- **Time to code:** 30 minutes
17+
18+
---
19+
20+
## Solution Exploration
21+
22+
### What solutions did I consider or miss?
23+
24+
- I explored using a greedy algorithm
25+
- Come to find out that Edit Distance is also known as Levenshtein Distance, which is usually solved using dynamic programming
26+
27+
### Analysis
28+
29+
- Using dynamic programming allows for remembering the distances at each of the characters for both words
30+
- Making sure to populate the table initially before iterating over
31+
- Comparing the minimum of the current character in the table with previous characters
32+
- Returning the correct index of the table
33+
34+
### Was my final solution optimal?
35+
36+
- Yes. Making use of the table and dynamic programming allows for making use of previous operations and calculations
37+
38+
### What triggers or patterns did I find or miss?
39+
40+
- I missed dynamic programming being the most efficient way of solving this distance problem
41+
42+
## Mistakes & Bugs
43+
44+
### Mistakes I keep making
45+
46+
- Not tracing through a simple example before starting to code
47+
48+
### Bugs to add to the Bug List
49+
50+
- Off by one iterations that go out of bounds
51+
52+
## Retrospective
53+
54+
### What could I have done differently?
55+
56+
- Setup a trace before going into the coding
57+
58+
## Rubric Self-Rating (1–5)
59+
60+
| Category | Rating | Notes |
61+
| ------------------- | ------ | ----- |
62+
| **Problem solving** | 2 | |
63+
| **Coding** | 3 | |
64+
| **Verification** | 2 | |
65+
| **Communication** | 3 | |
66+
67+
---
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [Edit Distance](https://leetcode.com/problems/edit-distance) ![Shield](https://img.shields.io/badge/Medium-orange)
2+
3+
Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_.
4+
5+
You have the following three operations permitted on a word:
6+
7+
- Insert a character
8+
- Delete a character
9+
- Replace a character
10+
11+
## Example 1
12+
13+
```bash
14+
Input: word1 = "horse", word2 = "ros"
15+
Output: 3
16+
Explanation:
17+
horse -> rorse (replace 'h' with 'r')
18+
rorse -> rose (remove 'r')
19+
rose -> ros (remove 'e')
20+
```
21+
22+
## Example 2
23+
24+
```bash
25+
Input: word1 = "intention", word2 = "execution"
26+
Output: 5
27+
Explanation:
28+
intention -> inention (remove 't')
29+
inention -> enention (replace 'i' with 'e')
30+
enention -> exention (replace 'n' with 'x')
31+
exention -> exection (replace 'n' with 'c')
32+
exection -> execution (insert 'u')
33+
```
34+
35+
## Constraints
36+
37+
- `0 <= word1.length, word2.length <= 500`
38+
- `word1` and `word2` consist of lowercase English letters.

0072-edit-distance/edit-distance.ts renamed to leetcode/medium/0072-edit-distance/edit-distance.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @time - O(m * n) where m is the length of word 1 and n is the length of word2
33
* @space - O(m * n) for the DP table
44
*/
5-
function minDistance(word1: string, word2: string): number {
5+
export function minDistance(word1: string, word2: string): number {
66
const m = word1.length;
77
const n = word2.length;
88

@@ -24,9 +24,9 @@ function minDistance(word1: string, word2: string): number {
2424
dp[i - 1][j - 1] + (word1[i - 1] === word2[j - 1] ? 0 : 1),
2525
dp[i - 1][j] + 1,
2626
dp[i][j - 1] + 1
27-
)
27+
);
2828
}
2929
}
3030

3131
return dp[m][n];
32-
};
32+
}

0 commit comments

Comments
 (0)