hello-algo/codes/swift/chapter_dynamic_programming/edit_distance.swift
nuomi1 9ea8a73059
Feature/chapter dynamic programming swift (#608)
* feat: add Swift codes for intro_to_dynamic_programming article

* feat: add Swift codes for dp_problem_features article

* feat: add Swift codes for dp_solution_pipeline article

* feat: add Swift codes for knapsack_problem article

* feat: add Swift codes for unbounded_knapsack_problem article

* feat: add Swift codes for edit_distance_problem article
2023-07-18 12:49:03 +08:00

147 lines
4.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* File: edit_distance.swift
* Created Time: 2023-07-16
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func editDistanceDFS(s: String, t: String, i: Int, j: Int) -> Int {
// s t 0
if i == 0, j == 0 {
return 0
}
// s t
if i == 0 {
return j
}
// t s
if j == 0 {
return i
}
//
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
return editDistanceDFS(s: s, t: t, i: i - 1, j: j - 1)
}
// = + 1
let insert = editDistanceDFS(s: s, t: t, i: i, j: j - 1)
let delete = editDistanceDFS(s: s, t: t, i: i - 1, j: j)
let replace = editDistanceDFS(s: s, t: t, i: i - 1, j: j - 1)
//
return min(min(insert, delete), replace) + 1
}
/* */
func editDistanceDFSMem(s: String, t: String, mem: inout [[Int]], i: Int, j: Int) -> Int {
// s t 0
if i == 0, j == 0 {
return 0
}
// s t
if i == 0 {
return j
}
// t s
if j == 0 {
return i
}
//
if mem[i][j] != -1 {
return mem[i][j]
}
//
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
return editDistanceDFS(s: s, t: t, i: i - 1, j: j - 1)
}
// = + 1
let insert = editDistanceDFS(s: s, t: t, i: i, j: j - 1)
let delete = editDistanceDFS(s: s, t: t, i: i - 1, j: j)
let replace = editDistanceDFS(s: s, t: t, i: i - 1, j: j - 1)
//
mem[i][j] = min(min(insert, delete), replace) + 1
return mem[i][j]
}
/* */
func editDistanceDP(s: String, t: String) -> Int {
let n = s.utf8CString.count
let m = t.utf8CString.count
var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)
//
for i in stride(from: 1, through: n, by: 1) {
dp[i][0] = i
}
for j in stride(from: 1, through: m, by: 1) {
dp[0][j] = j
}
//
for i in stride(from: 1, through: n, by: 1) {
for j in stride(from: 1, through: m, by: 1) {
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
//
dp[i][j] = dp[i - 1][j - 1]
} else {
// = + 1
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1
}
}
}
return dp[n][m]
}
/* */
func editDistanceDPComp(s: String, t: String) -> Int {
let n = s.utf8CString.count
let m = t.utf8CString.count
var dp = Array(repeating: 0, count: m + 1)
//
for j in stride(from: 1, through: m, by: 1) {
dp[j] = j
}
//
for i in stride(from: 1, through: n, by: 1) {
//
var leftup = dp[0] // dp[i-1, j-1]
dp[0] = i
//
for j in stride(from: 1, through: m, by: 1) {
let temp = dp[j]
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
//
dp[j] = leftup
} else {
// = + 1
dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1
}
leftup = temp // dp[i-1, j-1]
}
}
return dp[m]
}
@main
enum EditDistance {
/* Driver Code */
static func main() {
let s = "bag"
let t = "pack"
let n = s.utf8CString.count
let m = t.utf8CString.count
//
var res = editDistanceDFS(s: s, t: t, i: n, j: m)
print("\(s) 更改为 \(t) 最少需要编辑 \(res)")
//
var mem = Array(repeating: Array(repeating: -1, count: m + 1), count: n + 1)
res = editDistanceDFSMem(s: s, t: t, mem: &mem, i: n, j: m)
print("\(s) 更改为 \(t) 最少需要编辑 \(res)")
//
res = editDistanceDP(s: s, t: t)
print("\(s) 更改为 \(t) 最少需要编辑 \(res)")
//
res = editDistanceDPComp(s: s, t: t)
print("\(s) 更改为 \(t) 最少需要编辑 \(res)")
}
}