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