Skip to content

Commit d6b503e

Browse files
authored
Merge pull request #18 from iamAntimPal/Leetcode-75
Leetcode 75
2 parents 922cbb6 + bc1b58c commit d6b503e

File tree

2 files changed

+264
-0
lines changed

2 files changed

+264
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def minDistance(self, word1: str, word2: str) -> int:
3+
m, n = len(word1), len(word2)
4+
f = [[0] * (n + 1) for _ in range(m + 1)]
5+
for j in range(1, n + 1):
6+
f[0][j] = j
7+
for i, a in enumerate(word1, 1):
8+
f[i][0] = i
9+
for j, b in enumerate(word2, 1):
10+
if a == b:
11+
f[i][j] = f[i - 1][j - 1]
12+
else:
13+
f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1
14+
return f[m][n]
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
2+
<!-- problem:start -->
3+
4+
# [72. Edit Distance](https://leetcode.com/problems/edit-distance)
5+
6+
---
7+
- **comments**: true
8+
- **difficulty**: Medium
9+
- **tags**:
10+
- String
11+
- Dynamic Programming
12+
---
13+
14+
## Description
15+
16+
<!-- description:start -->
17+
18+
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p>
19+
20+
<p>You have the following three operations permitted on a word:</p>
21+
22+
<ul>
23+
<li>Insert a character</li>
24+
<li>Delete a character</li>
25+
<li>Replace a character</li>
26+
</ul>
27+
28+
<p>&nbsp;</p>
29+
<p><strong class="example">Example 1:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> word1 = &quot;horse&quot;, word2 = &quot;ros&quot;
33+
<strong>Output:</strong> 3
34+
<strong>Explanation:</strong>
35+
horse -&gt; rorse (replace &#39;h&#39; with &#39;r&#39;)
36+
rorse -&gt; rose (remove &#39;r&#39;)
37+
rose -&gt; ros (remove &#39;e&#39;)
38+
</pre>
39+
40+
<p><strong class="example">Example 2:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> word1 = &quot;intention&quot;, word2 = &quot;execution&quot;
44+
<strong>Output:</strong> 5
45+
<strong>Explanation:</strong>
46+
intention -&gt; inention (remove &#39;t&#39;)
47+
inention -&gt; enention (replace &#39;i&#39; with &#39;e&#39;)
48+
enention -&gt; exention (replace &#39;n&#39; with &#39;x&#39;)
49+
exention -&gt; exection (replace &#39;n&#39; with &#39;c&#39;)
50+
exection -&gt; execution (insert &#39;u&#39;)
51+
</pre>
52+
53+
<p>&nbsp;</p>
54+
<p><strong>Constraints:</strong></p>
55+
56+
<ul>
57+
<li><code>0 &lt;= word1.length, word2.length &lt;= 500</code></li>
58+
<li><code>word1</code> and <code>word2</code> consist of lowercase English letters.</li>
59+
</ul>
60+
61+
<!-- description:end -->
62+
63+
## Solutions
64+
65+
<!-- solution:start -->
66+
67+
### Solution 1: Dynamic Programming
68+
69+
We define $f[i][j]$ as the minimum number of operations to convert $word1$ of length $i$ to $word2$ of length $j$. $f[i][0] = i$, $f[0][j] = j$, $i \in [1, m], j \in [0, n]$.
70+
71+
We consider $f[i][j]$:
72+
73+
- If $word1[i - 1] = word2[j - 1]$, then we only need to consider the minimum number of operations to convert $word1$ of length $i - 1$ to $word2$ of length $j - 1$, so $f[i][j] = f[i - 1][j - 1]$;
74+
- Otherwise, we can consider insert, delete, and replace operations, then $f[i][j] = \min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1$.
75+
76+
Finally, we can get the state transition equation:
77+
78+
$$
79+
f[i][j] = \begin{cases}
80+
i, & \textit{if } j = 0 \\
81+
j, & \textit{if } i = 0 \\
82+
f[i - 1][j - 1], & \textit{if } word1[i - 1] = word2[j - 1] \\
83+
\min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1, & \textit{otherwise}
84+
\end{cases}
85+
$$
86+
87+
Finally, we return $f[m][n]$.
88+
89+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. $m$ and $n$ are the lengths of $word1$ and $word2$ respectively.
90+
91+
<!-- tabs:start -->
92+
93+
#### Python3
94+
95+
```python
96+
class Solution:
97+
def minDistance(self, word1: str, word2: str) -> int:
98+
m, n = len(word1), len(word2)
99+
f = [[0] * (n + 1) for _ in range(m + 1)]
100+
for j in range(1, n + 1):
101+
f[0][j] = j
102+
for i, a in enumerate(word1, 1):
103+
f[i][0] = i
104+
for j, b in enumerate(word2, 1):
105+
if a == b:
106+
f[i][j] = f[i - 1][j - 1]
107+
else:
108+
f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1
109+
return f[m][n]
110+
```
111+
112+
#### Java
113+
114+
```java
115+
class Solution {
116+
public int minDistance(String word1, String word2) {
117+
int m = word1.length(), n = word2.length();
118+
int[][] f = new int[m + 1][n + 1];
119+
for (int j = 1; j <= n; ++j) {
120+
f[0][j] = j;
121+
}
122+
for (int i = 1; i <= m; ++i) {
123+
f[i][0] = i;
124+
for (int j = 1; j <= n; ++j) {
125+
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
126+
f[i][j] = f[i - 1][j - 1];
127+
} else {
128+
f[i][j] = Math.min(f[i - 1][j], Math.min(f[i][j - 1], f[i - 1][j - 1])) + 1;
129+
}
130+
}
131+
}
132+
return f[m][n];
133+
}
134+
}
135+
```
136+
137+
#### C++
138+
139+
```cpp
140+
class Solution {
141+
public:
142+
int minDistance(string word1, string word2) {
143+
int m = word1.size(), n = word2.size();
144+
int f[m + 1][n + 1];
145+
for (int j = 0; j <= n; ++j) {
146+
f[0][j] = j;
147+
}
148+
for (int i = 1; i <= m; ++i) {
149+
f[i][0] = i;
150+
for (int j = 1; j <= n; ++j) {
151+
if (word1[i - 1] == word2[j - 1]) {
152+
f[i][j] = f[i - 1][j - 1];
153+
} else {
154+
f[i][j] = min({f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]}) + 1;
155+
}
156+
}
157+
}
158+
return f[m][n];
159+
}
160+
};
161+
```
162+
163+
#### Go
164+
165+
```go
166+
func minDistance(word1 string, word2 string) int {
167+
m, n := len(word1), len(word2)
168+
f := make([][]int, m+1)
169+
for i := range f {
170+
f[i] = make([]int, n+1)
171+
}
172+
for j := 1; j <= n; j++ {
173+
f[0][j] = j
174+
}
175+
for i := 1; i <= m; i++ {
176+
f[i][0] = i
177+
for j := 1; j <= n; j++ {
178+
if word1[i-1] == word2[j-1] {
179+
f[i][j] = f[i-1][j-1]
180+
} else {
181+
f[i][j] = min(f[i-1][j], min(f[i][j-1], f[i-1][j-1])) + 1
182+
}
183+
}
184+
}
185+
return f[m][n]
186+
}
187+
```
188+
189+
#### TypeScript
190+
191+
```ts
192+
function minDistance(word1: string, word2: string): number {
193+
const m = word1.length;
194+
const n = word2.length;
195+
const f: number[][] = Array(m + 1)
196+
.fill(0)
197+
.map(() => Array(n + 1).fill(0));
198+
for (let j = 1; j <= n; ++j) {
199+
f[0][j] = j;
200+
}
201+
for (let i = 1; i <= m; ++i) {
202+
f[i][0] = i;
203+
for (let j = 1; j <= n; ++j) {
204+
if (word1[i - 1] === word2[j - 1]) {
205+
f[i][j] = f[i - 1][j - 1];
206+
} else {
207+
f[i][j] = Math.min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1;
208+
}
209+
}
210+
}
211+
return f[m][n];
212+
}
213+
```
214+
215+
#### JavaScript
216+
217+
```js
218+
/**
219+
* @param {string} word1
220+
* @param {string} word2
221+
* @return {number}
222+
*/
223+
var minDistance = function (word1, word2) {
224+
const m = word1.length;
225+
const n = word2.length;
226+
const f = Array(m + 1)
227+
.fill(0)
228+
.map(() => Array(n + 1).fill(0));
229+
for (let j = 1; j <= n; ++j) {
230+
f[0][j] = j;
231+
}
232+
for (let i = 1; i <= m; ++i) {
233+
f[i][0] = i;
234+
for (let j = 1; j <= n; ++j) {
235+
if (word1[i - 1] === word2[j - 1]) {
236+
f[i][j] = f[i - 1][j - 1];
237+
} else {
238+
f[i][j] = Math.min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1;
239+
}
240+
}
241+
}
242+
return f[m][n];
243+
};
244+
```
245+
246+
<!-- tabs:end -->
247+
248+
<!-- solution:end -->
249+
250+
<!-- problem:end -->

0 commit comments

Comments
 (0)