add new solutions

This commit is contained in:
kanna5 2026-01-05 16:48:03 +09:00
parent 0c73608ce5
commit d798d5e8c9
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
19 changed files with 661 additions and 4 deletions

View file

@ -0,0 +1,36 @@
package q72
func make2d[T any](rows, cols int) [][]T {
alloc := make([]T, rows*cols)
ret := make([][]T, rows)
for i := range ret {
ret[i] = alloc[cols*i : cols*(i+1)]
}
return ret
}
func minDistance(word1 string, word2 string) int {
minDists := make2d[int](len(word1)+1, len(word2)+1)
for i1 := range len(word1) {
minDists[i1+1][0] = i1 + 1
}
for i2 := range len(word2) {
minDists[0][i2+1] = i2 + 1
}
for i1 := 1; i1 <= len(word1); i1++ {
for i2 := 1; i2 <= len(word2); i2++ {
if word1[i1-1] == word2[i2-1] {
minDists[i1][i2] = minDists[i1-1][i2-1]
} else {
minDists[i1][i2] = min(
minDists[i1-1][i2], minDists[i1][i2-1], minDists[i1-1][i2-1],
) + 1
}
}
}
return minDists[len(word1)][len(word2)]
}
var _ = minDistance