LeetcodeEdit Distance
Posted wuezs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetcodeEdit Distance相关的知识,希望对你有一定的参考价值。
题目链接:https://leetcode.com/problems/edit-distance/
题目:
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
思路:
c[i][j]表示word1的0~i子串和word2的0~j子串的最小编辑距离。状态转移方程:
如果sc[i]==tc[j]即最后一个字符相等 : c[i][j]=min{c[i-1][j],c[i][j-1],c[i-1][j-1]}
否则最后一个字符需要替换增加1个编辑距离:c[i][j]=min{c[i-1][j],c[i][j-1],c[i-1][j-1]+1}
算法: