-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path72.go
More file actions
48 lines (43 loc) · 745 Bytes
/
72.go
File metadata and controls
48 lines (43 loc) · 745 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
func minDistance(word1 string, word2 string) int {
if len(word1) == 0 {
return len(word2)
}
if len(word2) == 0 {
return len(word1)
}
m, n := len(word1), len(word2)
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
dp[i][0] = i
}
for j := 0; j < n+1; j++ {
dp[0][j] = j
}
for i := 1; i < m+1; i++ {
for j := 1; j < n+1; j++ {
if word1[i-1] != word2[j-1] {
dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+1)
} else {
dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1])
}
}
}
return dp[m][n]
}
func min(x, y, z int) int {
if x > y {
if y > z {
return z
} else {
return y
}
} else {
if x > z {
return z
} else {
return x
}
}
}