To solve the problem of finding the minimum number of operations (insertion, deletion, substitution) to convert string A to string B, we use the Levenshtein Distance algorithm, a classic dynamic programming approach.
The problem can be broken down into smaller subproblems where we compute the minimum operations for substrings of A and B. We define dp[i][j] as the minimum operations to convert the first i characters of A to the first j characters of B.
Base Cases:
i=0), we need j insertions to get B: dp[0][j] = j.j=0), we need i deletions to get B: dp[i][0] = i.Recursive Case:
i-th character of A equals the j-th character of B (A[i-1] == B[j-1]), no operation is needed: dp[i][j] = dp[i-1][j-1].dp[i-1][j-1] +1 (replace A[i-1] with B[j-1]).dp[i-1][j] +1 (delete A[i-1]).dp[i][j-1] +1 (insert B[j-1] into A).def min_edit_distance(A: str, B: str) -> int:
m, n = len(A), len(B)
dp = [[0]*(n+1) for _ in range(m+1)]
# Base cases
for i in range(m+1):
dp[i][0] = i
for j in range(n+1):
dp[0][j] = j
# Fill DP table
for i in range(1, m+1):
for j in range(1, n+1):
if A[i-1] == B[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(
dp[i-1][j-1] +1, # Replace
dp[i-1][j] +1, # Delete
dp[i][j-1] +1 # Insert
)
return dp[m][n]
We use a 1D array since each row only depends on the previous row:
def min_edit_distance_optimized(A: str, B: str) -> int:
if len(A) < len(B):
return min_edit_distance_optimized(B, A) # Ensure B is shorter
m, n = len(A), len(B)
dp = list(range(n+1)) # Base case: i=0
for i in range(1, m+1):
prev = dp[0] # dp[i-1][0]
dp[0] = i # Base case: j=0 for current i
for j in range(1, n+1):
temp = dp[j] # dp[i-1][j]
if A[i-1] == B[j-1]:
dp[j] = prev
else:
dp[j] = min(prev+1, dp[j]+1, dp[j-1]+1)
prev = temp # Update prev to dp[i-1][j] for next j
return dp[n]
Both approaches efficiently compute the minimum edit distance, with the optimized version being better for longer strings. For example:
min_edit_distance("kitten", "sitting") returns 3 (replace 'k'→'s', 'e'→'i', insert 'g').This solution is widely used in applications like spell checkers, DNA sequence alignment, and natural language processing.
Answer: The minimum number of operations is calculated using the Levenshtein Distance algorithm as described. For example, if the input strings are "kitten" and "sitting", the answer is 3. The general solution is implemented via dynamic programming as shown above.
(免責(zé)聲明:本文為本網(wǎng)站出于傳播商業(yè)信息之目的進(jìn)行轉(zhuǎn)載發(fā)布,不代表本網(wǎng)站的觀點(diǎn)及立場(chǎng)。本文所涉文、圖、音視頻等資料的一切權(quán)利和法律責(zé)任歸材料提供方所有和承擔(dān)。本網(wǎng)站對(duì)此資訊文字、圖片等所有信息的真實(shí)性不作任何保證或承諾,亦不構(gòu)成任何購(gòu)買(mǎi)、投資等建議,據(jù)此操作者風(fēng)險(xiǎn)自擔(dān)。) 本文為轉(zhuǎn)載內(nèi)容,授權(quán)事宜請(qǐng)聯(lián)系原著作權(quán)人,如有侵權(quán),請(qǐng)聯(lián)系本網(wǎng)進(jìn)行刪除。