11
11
12
12
<p ><strong >示例  ; 1:</strong ></p >
13
13
14
- <pre ><strong >输入:</strong >
14
+ <pre ><strong >输入:</strong >
15
15
first = " ; pale" ;
16
16
second = " ; ple" ;
17
17
<strong >输出:</strong > True</pre >
@@ -20,7 +20,7 @@ second = "ple"
20
20
21
21
<p ><strong >示例  ; 2:</strong ></p >
22
22
23
- <pre ><strong >输入:</strong >
23
+ <pre ><strong >输入:</strong >
24
24
first = " ; pales" ;
25
25
second = " ; pal" ;
26
26
<strong >输出:</strong > False
@@ -146,6 +146,55 @@ public:
146
146
};
147
147
```
148
148
149
+ ### **Go**
150
+
151
+ 可以直接扩展成[编辑距离](https://leetcode-cn.com/problems/edit-distance/)问题的解法
152
+
153
+ ```go
154
+ func oneEditAway(first string, second string) bool {
155
+ if first == second {
156
+ return true
157
+ }
158
+ m, n := len(first), len(second)
159
+ dp := make([][]int, m+1)
160
+ for i := 0; i <= m; i++ {
161
+ dp[i] = make([]int, n+1)
162
+ }
163
+ for i := 0; i <= m; i++ {
164
+ dp[i][0] = i
165
+ }
166
+ for j := 0; j <= n; j++ {
167
+ dp[0][j] = j
168
+ }
169
+ for i := 1; i <= m; i++ {
170
+ for j := 1; j <= n; j++ {
171
+ if first[i-1] == second[j-1] {
172
+ dp[i][j] = dp[i-1][j-1]
173
+ } else {
174
+ insert := dp[i][j-1] + 1
175
+ delete := dp[i-1][j] + 1
176
+ update := dp[i-1][j-1] + 1
177
+ dp[i][j] = min(insert, delete, update)
178
+ }
179
+ }
180
+ }
181
+ return dp[m][n] == 1
182
+ }
183
+
184
+ func min(x, y, z int) int {
185
+ if x < y {
186
+ if x < z {
187
+ return x
188
+ }
189
+ return z
190
+ }
191
+ if y < z {
192
+ return y
193
+ }
194
+ return z
195
+ }
196
+ ```
197
+
149
198
### ** ...**
150
199
151
200
```
0 commit comments