mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -6,100 +6,100 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
|
||||
def edit_distance_dfs(s: str, t: str, i: int, j: int) -> int:
|
||||
"""Edit distance: Brute-force search"""
|
||||
# If both s and t are empty, return 0
|
||||
"""Edit distance: brute-force search"""
|
||||
# If both prefixes s[:i] and t[:j] are empty, return 0
|
||||
if i == 0 and j == 0:
|
||||
return 0
|
||||
# If s is empty, return length of t
|
||||
# If prefix s[:i] is empty, return the length j of prefix t[:j]
|
||||
if i == 0:
|
||||
return j
|
||||
# If t is empty, return length of s
|
||||
# If prefix t[:j] is empty, return the length i of prefix s[:i]
|
||||
if j == 0:
|
||||
return i
|
||||
# If two characters are equal, skip both characters
|
||||
# If the two characters are equal, skip both of them
|
||||
if s[i - 1] == t[j - 1]:
|
||||
return edit_distance_dfs(s, t, i - 1, j - 1)
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
# Minimum edits = the minimum among the insertion, deletion, and replacement subproblems + 1
|
||||
insert = edit_distance_dfs(s, t, i, j - 1)
|
||||
delete = edit_distance_dfs(s, t, i - 1, j)
|
||||
replace = edit_distance_dfs(s, t, i - 1, j - 1)
|
||||
# Return minimum edit steps
|
||||
# Return the minimum number of edits
|
||||
return min(insert, delete, replace) + 1
|
||||
|
||||
|
||||
def edit_distance_dfs_mem(s: str, t: str, mem: list[list[int]], i: int, j: int) -> int:
|
||||
"""Edit distance: Memoization search"""
|
||||
# If both s and t are empty, return 0
|
||||
"""Edit distance: memoized search"""
|
||||
# If both prefixes s[:i] and t[:j] are empty, return 0
|
||||
if i == 0 and j == 0:
|
||||
return 0
|
||||
# If s is empty, return length of t
|
||||
# If prefix s[:i] is empty, return the length j of prefix t[:j]
|
||||
if i == 0:
|
||||
return j
|
||||
# If t is empty, return length of s
|
||||
# If prefix t[:j] is empty, return the length i of prefix s[:i]
|
||||
if j == 0:
|
||||
return i
|
||||
# If there's a record, return it directly
|
||||
# If a result has already been recorded, return it directly
|
||||
if mem[i][j] != -1:
|
||||
return mem[i][j]
|
||||
# If two characters are equal, skip both characters
|
||||
# If the two characters are equal, skip both of them
|
||||
if s[i - 1] == t[j - 1]:
|
||||
return edit_distance_dfs_mem(s, t, mem, i - 1, j - 1)
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
# Minimum edits = the minimum among the insertion, deletion, and replacement subproblems + 1
|
||||
insert = edit_distance_dfs_mem(s, t, mem, i, j - 1)
|
||||
delete = edit_distance_dfs_mem(s, t, mem, i - 1, j)
|
||||
replace = edit_distance_dfs_mem(s, t, mem, i - 1, j - 1)
|
||||
# Record and return minimum edit steps
|
||||
# Record and return the minimum number of edits
|
||||
mem[i][j] = min(insert, delete, replace) + 1
|
||||
return mem[i][j]
|
||||
|
||||
|
||||
def edit_distance_dp(s: str, t: str) -> int:
|
||||
"""Edit distance: Dynamic programming"""
|
||||
"""Edit distance: dynamic programming"""
|
||||
n, m = len(s), len(t)
|
||||
dp = [[0] * (m + 1) for _ in range(n + 1)]
|
||||
# State transition: first row and first column
|
||||
# State transitions: first row and first column
|
||||
for i in range(1, n + 1):
|
||||
dp[i][0] = i
|
||||
for j in range(1, m + 1):
|
||||
dp[0][j] = j
|
||||
# State transition: rest of the rows and columns
|
||||
# State transitions: remaining rows and columns
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, m + 1):
|
||||
if s[i - 1] == t[j - 1]:
|
||||
# If two characters are equal, skip both characters
|
||||
# If the two characters are equal, skip both of them
|
||||
dp[i][j] = dp[i - 1][j - 1]
|
||||
else:
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
# Minimum edits = the minimum among the insertion, deletion, and replacement subproblems + 1
|
||||
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
|
||||
return dp[n][m]
|
||||
|
||||
|
||||
def edit_distance_dp_comp(s: str, t: str) -> int:
|
||||
"""Edit distance: Space-optimized dynamic programming"""
|
||||
"""Edit distance: space-optimized dynamic programming"""
|
||||
n, m = len(s), len(t)
|
||||
dp = [0] * (m + 1)
|
||||
# State transition: first row
|
||||
# State transitions: first row
|
||||
for j in range(1, m + 1):
|
||||
dp[j] = j
|
||||
# State transition: rest of the rows
|
||||
# State transitions: remaining rows
|
||||
for i in range(1, n + 1):
|
||||
# State transition: first column
|
||||
# State transitions: first column
|
||||
leftup = dp[0] # Temporarily store dp[i-1, j-1]
|
||||
dp[0] += 1
|
||||
# State transition: rest of the columns
|
||||
# State transitions: remaining columns
|
||||
for j in range(1, m + 1):
|
||||
temp = dp[j]
|
||||
if s[i - 1] == t[j - 1]:
|
||||
# If two characters are equal, skip both characters
|
||||
# If the two characters are equal, skip both of them
|
||||
dp[j] = leftup
|
||||
else:
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
# Minimum edits = the minimum among the insertion, deletion, and replacement subproblems + 1
|
||||
dp[j] = min(dp[j - 1], dp[j], leftup) + 1
|
||||
leftup = temp # Update for next round's dp[i-1, j-1]
|
||||
leftup = temp # Update to dp[i-1, j-1] for the next iteration
|
||||
return dp[m]
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
s = "bag"
|
||||
t = "pack"
|
||||
@@ -107,17 +107,17 @@ if __name__ == "__main__":
|
||||
|
||||
# Brute-force search
|
||||
res = edit_distance_dfs(s, t, n, m)
|
||||
print(f"Changing {s} to {t} requires a minimum of {res} edits")
|
||||
print(f"Changing {s} to {t} requires at least {res} edits")
|
||||
|
||||
# Memoization search
|
||||
# Memoized search
|
||||
mem = [[-1] * (m + 1) for _ in range(n + 1)]
|
||||
res = edit_distance_dfs_mem(s, t, mem, n, m)
|
||||
print(f"Changing {s} to {t} requires a minimum of {res} edits")
|
||||
print(f"Changing {s} to {t} requires at least {res} edits")
|
||||
|
||||
# Dynamic programming
|
||||
res = edit_distance_dp(s, t)
|
||||
print(f"Changing {s} to {t} requires a minimum of {res} edits")
|
||||
print(f"Changing {s} to {t} requires at least {res} edits")
|
||||
|
||||
# Space-optimized dynamic programming
|
||||
res = edit_distance_dp_comp(s, t)
|
||||
print(f"Changing {s} to {t} requires a minimum of {res} edits")
|
||||
print(f"Changing {s} to {t} requires at least {res} edits")
|
||||
|
||||
Reference in New Issue
Block a user